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


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


Character 类的 isWhitespace(char ch) 方法确定给定的字符(Unicode 代码点)是否为空白字符。

如果满足以下条件之一,则可以将 Java 中的字符视为空白字符:

  • 该字符是 Unicode 空格字符(SPACE_SEPARATOR、LINE_SEPARATOR 或 PARAGRAPH_SEPARATOR),但不能是不间断空格。
  • 字符必须是水平制表。
  • 字符必须是换行符。
  • 字符必须是垂直制表。
  • 字符必须是 FORM FEED。
  • 字符必须是回车符。
  • 字符必须是文件分隔符。
  • 字符必须是组分隔符。
  • 字符必须是记录分隔符。
  • 字符必须是单位分隔符。

用法

public static boolean isWhitespace(char ch)

参数

上述方法只需要一个参数:

a.) 需要测试的字符。

返回值

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

例子1

public class JavaCharacterisWhitespaceExample1 {
    public static void main(String[] args) {
        // Create three char primitives:ch1, ch2 and ch3
        char ch1, ch2, ch3
                ;
        // Assign the values to the char primitives.
        ch1 = '\n';
        ch2 = 'f';
        ch3 = '\b';
        
        //Create three boolean primitives.
        boolean b1, b2, b3;
       b1 =  Character.isWhitespace(ch1);
       b2 =  Character.isWhitespace(ch2);
       b3 =  Character.isWhitespace(ch3);
      System.out.println("The first character is a white space character:"+b1);
      System.out.println("The second character is a white space character:"+b2);
      System.out.println("The third character is a white space character:"+b3);
   }
}

输出:

The first character is a white space character:true
The second character is a white space character:false
The third character is a white space character:false

例子2

public class JavaCharacterisWhitespaceExample2 {
    public static void main(String[] args) {
        // Create three char primitives:ch1, ch2 and ch3
        char ch1, ch2, ch3;	
                
        // Assign the values to the char primitives.
        ch1 = '\t';
        ch2 = ' ';
        ch3 = '\b';
        
        //Create three boolean primitives.
        boolean b1, b2, b3;
       b1 =  Character.isWhitespace(ch1);
       b2 =  Character.isWhitespace(ch2);
       b3 =  Character.isWhitespace(ch3);
      System.out.println("The first character is a white space character:"+b1);
      System.out.println("The second character is a white space character:"+b2);
      System.out.println("The third character is a white space character:"+b3);
   }
}

输出:

The first character is a white space character:true
The second character is a white space character:true
The third character is a white space character:false

Java 字符 isWhitespace() 方法

Character 类的 isWhitespace(int codePoint) 方法确定给定(或指定)字符是否为空白字符。

如果满足以下条件之一,则可以将 Java 中的字符视为空白字符:

  • 该字符是 Unicode 空格字符(SPACE_SEPARATOR、LINE_SEPARATOR 或 PARAGRAPH_SEPARATOR),但不能是不间断空格。
  • 字符必须是水平制表。
  • 字符必须是换行符。
  • 字符必须是垂直制表。
  • 字符必须是 FORM FEED。
  • 字符必须是回车符。
  • 字符必须是文件分隔符。
  • 字符必须是组分隔符。
  • 字符必须是记录分隔符。
  • 字符必须是单位分隔符。

用法

public static boolean isWhitespace(int codePoint)

参数

上述方法只需要一个参数:

a.)codePoint 是需要测试的字符。

返回值

isWhitespace(int codePoint) 方法返回一个布尔值,即如果给定(或指定)字符是 Java 空白字符,则返回 true。否则,此方法返回 false。

例子1

public class JavaCharacterisWhitespaceExample_1 {
    public static void main(String[] args) {
        // Initialize three codepoints:cp1, cp2 and cp3
                int cp1 = 49;
                int cp2 = 121;
                int cp3 = 234;
       // Check whether the codepoints are whitespaces or not.
	        boolean check1 = Character.isWhitespace(cp1);
                boolean check2 = Character.isWhitespace(cp2);
                boolean check3 = Character.isWhitespace(cp3);
       // Print the result.
		if(check1){
		System.out.print("The codepoint \'"+cp1+"\' is a whitespace character.\n");
		}
		else{
		System.out.print("The codePoint \'"+cp1+"\' is not a whitespace character.\n");
		}
                
		if(check2){
		System.out.print("The codepoint \'"+cp2+"\' is a whitespace character.\n");
		}
		else{
		System.out.print("The codePoint \'"+cp2+"\' is not a whitespace character.\n");
		}
                
                              if(check3){
		System.out.print("The codepoint \'"+cp3+"\' is a whitespace character.\n");
		}
		else{
		System.out.print("The codePoint \'"+cp3+"\' is not a whitespace character.\n");
	}
    }
}

输出:

The codePoint '49' is not a whitespace character.
The codePoint '121' is not a whitespace character.
The codePoint '234' is not a whitespace character.

例子2

public class JavaCharacterisWhitespaceExample_2 {
    public static void main(String[] args) {
        // Initialize three codepoints:cp1, cp2 and cp3
                int cp1 = 9;
                int cp2 = 10;
                int cp3 = 13;
       // Check whether the codepoints are whitespaces or not.
                boolean check1 = Character.isWhitespace(cp1);
                boolean check2 = Character.isWhitespace(cp2);
                boolean check3 = Character.isWhitespace(cp3);
        // Print the result.
            if(check1){
  System.out.print("The codepoint \'"+cp1+"\' is a whitespace character.\n");
                       }
            else{
System.out.print("The codePoint \'"+cp1+"\' is not a whitespace character.\n");
                       }
                
            if(check2){
System.out.print("The codepoint \'"+cp2+"\' is a whitespace character.\n");
                       }
             else{
System.out.print("The codePoint \'"+cp2+"\' is not a whitespace character.\n");
                  }
                
            if(check3){
System.out.print("The codepoint \'"+cp3+"\' is a whitespace character.\n");
                   }
             else{
System.out.print("The codePoint \'"+cp3+"\' is not a whitespace character.\n");
         }
    }
}

输出:

The codepoint '9' is a whitespace character.
The codepoint '10' is a whitespace character.
The codepoint '13' is a whitespace character.

例子3

public class JavaCharacterisWhitespaceExample_3 {
    public static void main(String[] args) {
        // Initialize three codepoints:cp1, cp2 and cp3
                int cp1 = 9;
                int cp2 = 121;
                int cp3 = 245;
      // check whether the codepoints are whitespaces or not.
               boolean check1 = Character.isWhitespace(cp1);
                boolean check2 = Character.isWhitespace(cp2);
                boolean check3 = Character.isWhitespace(cp3);
        // Print the result.
System.out.println("The first codePoint '"+cp1+"' is a Java space character:"+check1);	
System.out.println("The first codePoint '"+cp2+"' is a Java space character:"+check2);	
System.out.println("The first codePoint '"+cp3+"' is a Java space character:"+check3);	
    }
}

输出:

The first codePoint '9' is a Java space character:true
The first codePoint '121' is a Java space character:false
The first codePoint '245' is a Java space character:false




相关用法


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