当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Character isSpaceChar()用法及代码示例


Character 类的 isSpaceChar(char ch) 方法确定给定(或指定)字符是否为 Unicode 空格字符。

如果给定(或指定)字符被 Unicode 标准指定为空格字符,则该字符被视为空格字符。

如果字符的一般类别是以下之一,则上述方法返回 true:

  • SPACE_SEPARATOR
  • LINE_SEPARATOR
  • PARAGRAPH_SEPARATOR

用法

public static boolean isSpaceChar(char ch)

参数

ch: 这是需要测试的字符。

返回值

isSpaceChar(char ch) 方法返回一个布尔值,即如果给定(或指定)字符是空格字符,则返回 true。否则,该方法返回 false。

例子1

public class JavaCharacterIsSapceCharExample1 {
     public static void main(String[] args) {
      //Create three char primitives ch1, ch2 and ch3.
      char ch1, ch2, ch3;

      //Assign the values to ch1, ch2 and ch3.
      ch1 = ' ';
      ch2 = '\u2028';
      ch3 = '\u2078';

      // Create three boolean primitives b1, b2 and b3.
      boolean b1, b2, b3;
      
     //Check whether the characters ch1, ch2 and ch3 are space characters or not.
      b1 = Character.isSpaceChar(ch1);
      b2 = Character.isSpaceChar(ch2);
      b3 = Character.isSpaceChar(ch3);

      String str1 = "The first character is a space character:" + b1;
      String str2 = "The second character is a space character:" + b2;
      String str3 = "The third character is a space character:" + b3;

      // Print the values of b1, b2 and b3. 
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str2 );
   }
}

输出:

The first character is a space character:true
The second character is a space character:true
The second character is a space character:true

例子2

public class JavaCharacterIsSapceCharExample2 {
   public static void main(String[] args)
    {
        // Create two char primitives ch1, ch2 and assign the values to them.
        char ch1 = '$'; 
        char ch2 ='\u2025';
 
        // Check whether the character is a space character or not.
        boolean b1 = Character.isSpaceChar(ch1);
        boolean b2 = Character.isSpaceChar(ch2);
        System.out.println("The first character is a space character:" + b1);
        System.out.println("The first character is a space character:" + b2);
    }
}

输出:

The first character is a space character:false
The first character is a space character:false

例子3

import java.util.Scanner;
public class JavaCharacterIsSapceCharExample3 {
   public static void main(String[] args) {
    // Ask the user for the first input. 
       System.out.print("Enter the first input:");
    // Use the Scanner class to get the user input.
       Scanner s1 = new Scanner(System.in);
    // Gets the user input.
       char[] value1 = s1.nextLine().toCharArray();
    // Check whether the user input is a space character or not.
       for (char ch1:value1) {
   boolean result1 = Character.isSpaceChar(ch1);
    // Print the result.
       if(result1){
System.out.println("The character '" + ch1 + "' is a space character. ");				
                  }
     else{
System.out.println("The character '" + ch1 + "' is not a space character.");
           }		
       System.out.print("Enter the second input:");
       Scanner s2 = new Scanner(System.in);
       char[] value2 = s2.nextLine().toCharArray();
       for (char ch2:value2) {
      boolean result2 = Character.isSpaceChar(ch2);
      if(result2){
System.out.println("The character '" + ch2 + "' is a space character. ");				
                }
      else{
System.out.println("The character '" + ch2 + "' is a space character.");
        }		
      System.out.print("Enter the third input:");
       Scanner s3 = new Scanner(System.in);
       char[] value3 = s3.nextLine().toCharArray();
       for (char ch3:value3) {
      boolean result3 = Character.isSpaceChar(ch3);
      if(result2){
System.out.println("The character '" + ch3 + "' is a space character. ");				
                }
      else{
System.out.println("The character '" + ch3 + "' is not a space character.");
           }		
         }
       }
     }
  }
}

输出:

Enter the first input:_
The character '_' is not a space character.
Enter the second input:}
The character '}' is a space character.
Enter the third input:*
The character '*' is not a space character.




相关用法


注:本文由纯净天空筛选整理自 Java Character isSpaceChar() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。