Java中的字符.isWhitespace() 方法及示例
Java.lang。 字符.isWhitespace()是Java中的内置方法,根据Java确定指定字符(Unicode 代码点)是否为空白。一个字符是一个Java空白字符当且仅当它满足以下条件之一:
- 它是 Unicode 空格字符(SPACE_SEPARATOR、LINE_SEPARATOR 或 PARAGRAPH_SEPARATOR),但也不是不间断空格('\u00A0'、'\u2007'、'\u202F')。
- 它是'\t',U+0009 水平制表。
- 它是'\n',U+000A LINE FEED。
- 它是'\u000B',U+000B 垂直制表。
- 它是'\f',U+000C 换页。
- 它是'\r',U+000D 回车。
- 它是 '\u001C',U+001C 文件分隔符。
- 它是 '\u001D',U+001D 组分隔符。
- 它是 '\u001E',U+001E 记录分隔符。
- 它是 '\u001F',U+001F 单位分隔符。
句法:
public static boolean isWhitespace(datatype character)
参数:该函数接受一个强制参数字符。此参数的数据类型可以是 int 或 char。它指定要测试的字符。
返回值:如果字符是Java空白字符,此方法返回true,否则返回false。
下面的程序说明了字符.isWhitespace(char ch) 方法:
方案一:
// 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
方案二:
// 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