Java - Character isWhitespace() 方法



描述

Java 的Character isWhitespace() 方法用于根据 Java 标准判断指定的输入字符是否为空格。

以下任何字符都可被视为空格字符:

  • Unicode 空格字符(SPACE_SEPARATOR、LINE_SEPARATOR 或 PARAGRAPH_SEPARATOR),但不包括不换行空格 ('\u00A0', '\u2007', '\u202F')。

  • '\t', U+0009 − 水平制表符。

  • '\n', U+000A − 换行符。

  • '\u000B', U+000B − 垂直制表符。

  • '\f', U+000C − 换页符。

  • '\r', U+000D − 回车符。

  • '\u001C', U+001C − 文件分隔符。

  • '\u001D', U+001D − 分组分隔符。

  • '\u001E', U+001E − 记录分隔符。

  • '\u001F', U+001F − 单元分隔符。

此方法具有两种多态形式,参数不同,但返回类型相同。

语法

以下是 Java Character isWhitespace() 方法的语法

public static boolean isWhitespace(char ch)
(or)
public static boolean isWhitespace(int codePoint)

参数

  • ch − 要测试的字符

  • codePoint − 要测试的 Unicode 代码点

返回值

如果字符是 Java 空格字符,则此方法返回 true;否则返回 false。

检查字符是否为空格的示例

以下示例演示了 Java Character isWhitespace(char ch) 方法的使用。在这个程序中,我们创建了两个 char 变量,并分别为它们赋值一个空格和一个换页符。然后使用 isWhitespace() 方法检查这两个 char 变量,并将结果打印出来。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 char primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = ' ';
      ch2 = '\f';

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if ch1, ch2 are whitespace characters and 
       *  assign results to b1, b2
       */
      b1 = Character.isWhitespace(ch1);
      b2 = Character.isWhitespace(ch2);
      String str1 = "ch1 is a Java whitespace character is " + b1;
      String str2 = "ch2 is a Java whitespace character is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出

让我们编译并运行上面的程序,这将产生以下结果:

ch1 is a Java whitespace character is true
ch2 is a Java whitespace character is true

检查代码点是否为空格的示例

以下示例演示了 Java Character isWhitespace(int codepoint) 方法的使用。在这个程序中,我们创建了两个 int 变量,并分别为它们赋值 Unicode 值。然后使用 isWhitespace() 方法检查这两个 int 变量,并将结果打印出来。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x001c; // represents FILE SEPARATOR
      cp2 = 0x0abc;

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      /**
       *  check if cp1, cp2 represent whitespace characters 
       *  and assign results to b1, b2
       */
      b1 = Character.isWhitespace(cp1);
      b2 = Character.isWhitespace(cp2);
      String str1 = "cp1 represents Java whitespace character is " + b1;
      String str2 = "cp2 represents Java whitespace character is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出

让我们编译并运行上面的程序,这将产生以下结果:

cp1 represents Java whitespace character is true
cp2 represents Java whitespace character is false

检查代码点/字符是否为空格的示例

下面给出了另一个示例,展示如何在程序中使用此方法。我们使用换行符和制表符作为参数,并检查返回值。

package com.tutorialspoint;

public class WhitespaceDemo {
   public static void main(String args[]) {
      char ch = '\n';
      int cp = 0x0009;
      boolean b1 = Character.isWhitespace(ch);
      boolean b2 = Character.isWhitespace(cp);
      System.out.println(ch + " is a Whitespace: " + b1);
      System.out.println((char)cp + " is a Whitespace: " + b2);
   }
}

输出

让我们编译并运行上面的程序,输出将如下所示:

is a Whitespace: true
is a Whitespace: true

检查字符数组中的字符是否为空格的示例

此方法还可以通过传递字符数组的每个元素来调用,以单独检查它们是否为空格字符。这可以使用循环语句实现。然后,该方法用作条件语句的条件。

package com.tutorialspoint;

public class WhitespaceDemo {
   public static void main(String args[]) {
      char ch[] = {'\u001C', '\u001D', '\u025E', '\u001F'};
      for(int i = 0; i < ch.length; i++) {
         if(Character.isWhitespace(ch[i]))
            System.out.println(ch[i] + " is a white space");
         else
            System.out.println(ch[i] + " is not a white space");
      }
   }
}

输出

编译并执行上述程序后,输出将显示如下:

is a white space
 is a white space
ɞ is not a white space
 is a white space
java_lang_character.htm
广告
© . All rights reserved.