Java Character isWhitespace()方法及示例

Java Character isWhitespace()方法及示例

java.lang.Character.isWhitespace() 是java中的一个内置方法,用于确定指定的字符(Unicode代码点)是否符合Java的空白。一个字符是一个Java空白字符,当且仅当它满足以下条件之一。

  • 它是一个Unicode空格字符(SPACE_SEPARATOR、LINE_SEPARATOR或PARAGRAPH_SEPARATOR),但不同时是一个非断裂空格(’\u00A0’、’\u2007’、’\u202F’)。
  • 它是’\t’,U+0009 横向分页法。
  • 它是 ‘\n’, U+000A LINE FEED.
  • 它是 ‘\u000B’, U+000B VERTICAL TABULATION.
  • 它是 ‘\f’, U+000C FORM FEED.
  • 它是 ‘\r’, U+000D CARRIAGE RETURN.
  • 它是 ‘\u001C’, U+001C FILE SEPARATOR.
  • 它是 ‘\u001D’, U+001D GROUP SEPARATOR.
  • 它是 ‘\u001E’, U+001E RECORD SEPARATOR.
  • 它是 ‘\u001F’, U+001F UNIT SEPARATOR.

语法

public static boolean isWhitespace(datatype character)

参数 :该函数接受一个强制性参数 字符。 这个参数可以是数据类型int或char。它指定了要测试的字符。

返回值: 如果该字符是一个Java空白字符,该方法返回true,否则返回false。

以下程序说明了Character.isWhitespace(char ch)方法。

程序1 :

// Java program to illustrate the Character.isWhitespace()
// method when the passed parameter is a character
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // create 2 char primitives c1, c2
        char c1 = '*', c2 = '\f';
  
        boolean b1 = Character.isWhitespace(c1);
        boolean b2 = Character.isWhitespace(c2);
  
        String str1 = "c1 is a Java whitespace character is " + b1;
        String str2 = "c2 is a Java whitespace character is " + b2;
  
        // print b1, b2 values
        System.out.println(str1);
        System.out.println(str2);
    }
}

输出:

c1 is a Java whitespace character is false
c2 is a Java whitespace character is true

程序2

// Java program to demonstrate the Character.isWhitespace()
// method when the passed parameter is a character
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // create 2 char primitives c1, c2
        char c1 = '/', c2 = '\f';
  
        boolean b1 = Character.isWhitespace(c1);
        boolean b2 = Character.isWhitespace(c2);
  
        String str1 = "c1 is a Java whitespace character is " + b1;
        String str2 = "c2 is a Java whitespace character is " + b2;
  
        // print b1, b2 values
        System.out.println(str1);
        System.out.println(str2);
    }
}

输出:

c1 is a Java whitespace character is false
c2 is a Java whitespace character is true

程序3: 当参数为int类型时。

// Java program to demonstrate the
// Character.isWhitespace()
// method when the passed parameter
// is a character
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // create 2 int primitives c1, c2
        int c1 = 0x451c, c2 = 0x4abc;
  
        boolean b1 = Character.isWhitespace(c1);
        boolean b2 = Character.isWhitespace(c2);
  
        String str1 = "c1 represents Java whitespace character is " + b1;
        String str2 = "c2 represents Java whitespace character is " + b2;
  
        // print b1, b2 values
        System.out.println(str1);
        System.out.println(str2);
    }
}

输出:

c1 represents Java whitespace character is false
c2 represents Java whitespace character is false

程序4: 当参数为int类型时。

// Java program to demonstrate the Character.isWhitespace()
// method when the passed parameter is a character
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // create 2 int primitives c1, c2
        int c1 = 0x001c, c2 = 0x1bbc;
  
        boolean b1 = Character.isWhitespace(c1);
        boolean b2 = Character.isWhitespace(c2);
  
        String str1 = "c1 represents Java whitespace character is " + b1;
        String str2 = "c2 represents Java whitespace character is " + b2;
  
        // print b1, b2 values
        System.out.println(str1);
        System.out.println(str2);
    }
}

输出:

c1 represents Java whitespace character is true
c2 represents Java whitespace character is false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南