Character 類的 isSpaceChar(char ch) 方法確定給定(或指定)字符是否為 Unicode 空格字符。
如果給定(或指定)字符被 Unicode 標準指定為空格字符,則該字符被視為空格字符。
如果字符的一般類別是以下之一,則上述方法返回 true:
- SPACE_SEPARATOR
- LINE_SEPARATOR
- PARAGRAPH_SEPARATOR
用法
public static boolean isSpaceChar(char ch)
參數
ch: 這是需要測試的字符。
返回值
isSpaceChar(char ch) 方法返回一個布爾值,即如果給定(或指定)字符是空格字符,則返回 true。否則,該方法返回 false。
例子1
public class JavaCharacterIsSapceCharExample1 {
public static void main(String[] args) {
//Create three char primitives ch1, ch2 and ch3.
char ch1, ch2, ch3;
//Assign the values to ch1, ch2 and ch3.
ch1 = ' ';
ch2 = '\u2028';
ch3 = '\u2078';
// Create three boolean primitives b1, b2 and b3.
boolean b1, b2, b3;
//Check whether the characters ch1, ch2 and ch3 are space characters or not.
b1 = Character.isSpaceChar(ch1);
b2 = Character.isSpaceChar(ch2);
b3 = Character.isSpaceChar(ch3);
String str1 = "The first character is a space character:" + b1;
String str2 = "The second character is a space character:" + b2;
String str3 = "The third character is a space character:" + b3;
// Print the values of b1, b2 and b3.
System.out.println( str1 );
System.out.println( str2 );
System.out.println( str2 );
}
}
輸出:
The first character is a space character:true The second character is a space character:true The second character is a space character:true
例子2
public class JavaCharacterIsSapceCharExample2 {
public static void main(String[] args)
{
// Create two char primitives ch1, ch2 and assign the values to them.
char ch1 = '$';
char ch2 ='\u2025';
// Check whether the character is a space character or not.
boolean b1 = Character.isSpaceChar(ch1);
boolean b2 = Character.isSpaceChar(ch2);
System.out.println("The first character is a space character:" + b1);
System.out.println("The first character is a space character:" + b2);
}
}
輸出:
The first character is a space character:false The first character is a space character:false
例子3
import java.util.Scanner;
public class JavaCharacterIsSapceCharExample3 {
public static void main(String[] args) {
// Ask the user for the first input.
System.out.print("Enter the first input:");
// Use the Scanner class to get the user input.
Scanner s1 = new Scanner(System.in);
// Gets the user input.
char[] value1 = s1.nextLine().toCharArray();
// Check whether the user input is a space character or not.
for (char ch1:value1) {
boolean result1 = Character.isSpaceChar(ch1);
// Print the result.
if(result1){
System.out.println("The character '" + ch1 + "' is a space character. ");
}
else{
System.out.println("The character '" + ch1 + "' is not a space character.");
}
System.out.print("Enter the second input:");
Scanner s2 = new Scanner(System.in);
char[] value2 = s2.nextLine().toCharArray();
for (char ch2:value2) {
boolean result2 = Character.isSpaceChar(ch2);
if(result2){
System.out.println("The character '" + ch2 + "' is a space character. ");
}
else{
System.out.println("The character '" + ch2 + "' is a space character.");
}
System.out.print("Enter the third input:");
Scanner s3 = new Scanner(System.in);
char[] value3 = s3.nextLine().toCharArray();
for (char ch3:value3) {
boolean result3 = Character.isSpaceChar(ch3);
if(result2){
System.out.println("The character '" + ch3 + "' is a space character. ");
}
else{
System.out.println("The character '" + ch3 + "' is not a space character.");
}
}
}
}
}
}
輸出:
Enter the first input:_ The character '_' is not a space character. Enter the second input:} The character '}' is a space character. Enter the third input:* The character '*' is not a space character.
相關用法
- Java Character isSpace()用法及代碼示例
- Java Character isLetter()用法及代碼示例
- Java Character isAlphabetic()用法及代碼示例
- Java Character isValidCodePoint()用法及代碼示例
- Java Character isISOControl()用法及代碼示例
- 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 isWhitespace()用法及代碼示例
- Java Character isDefined()用法及代碼示例
- Java Character isJavaIdentifierstart()用法及代碼示例
- Java Character isHighSurrogate()用法及代碼示例
- Java Character isLetterOrDigit()用法及代碼示例
- Java Character codePointCount()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Character isSpaceChar() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。