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 isLetter()用法及代碼示例
- Java Character isAlphabetic()用法及代碼示例
- Java Character isValidCodePoint()用法及代碼示例
- Java Character isISOControl()用法及代碼示例
- Java Character isSpace()用法及代碼示例
- Java Character isMirrored()用法及代碼示例
- Java Character isBmpCodePoint()用法及代碼示例
- Java Character isIdentifierIgnorable()用法及代碼示例
- Java Character isDigit()用法及代碼示例
- Java Character isLowerCase()用法及代碼示例
- Java Character isJavaIdentifierPart()用法及代碼示例
- Java Character isUpperCase()用法及代碼示例
- Java Character isLowSurrogate()用法及代碼示例
- Java Character isJavaLetterOrDigit()用法及代碼示例
- Java Character isDefined()用法及代碼示例
- Java Character isJavaIdentifierstart()用法及代碼示例
- Java Character isHighSurrogate()用法及代碼示例
- Java Character isLetterOrDigit()用法及代碼示例
- Java Character isSpaceChar()用法及代碼示例
- Java Character codePointCount()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Character isWhitespace() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。