字符類的 isJavaIdentifierPart(char ch) 方法確定給定(或指定)字符是否是 Java 標識符的一部分。
如果以下任一條件為真,則給定字符是 Java 標識符的一部分:
- 字符是一個字母。
- 該字符是貨幣符號(如 '$')。
- 該字符為連接標點字符(如 '_')。
- 字符是一個數字。
- 字符為數字字母(如羅馬數字字符)。
- 字符是組合標記。
- 字符是非空格標記。
- isIdentifierIgnorable() 方法為特定字符返回真。
用法
public static boolean isJavaIdentifierPart(char ch)
參數
ch: 這是需要測試的字符。
返回值
如果給定(或指定)字符是 Java 標識符的一部分,則 isJavaIdentifierPart(char ch) 方法返回一個布爾值,即 true。否則,該方法返回 false。
例子1
import java.util.Scanner;
public class JavaCharacterisJavaIdentifierPartExample_1 {
public static void main(String[] args) {
// Ask the user to enter the input.
System.out.print("Enter the first input:");
// Use scanner 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 part Java Identifier or not.
for (char ch1:value1) {
boolean result1 = Character.isJavaIdentifierPart(ch1);
// Print the result.
if(result1){
System.out.println("The character '" + ch1 + "' is a part of Java Identifier. ");
}
else{
System.out.println("The character '" + ch1 + "' is not a part of Java Identifier. ");
}
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.isJavaIdentifierPart(ch2);
if(result2){
System.out.println("The character '" + ch2 + "' is a part of Java Identifier. ");
}
else{
System.out.println("The character '" + ch2 + "' is not a part of Java Identifier. ");
}
}
}
}
}
輸出:
Enter the first input:& The character '&' is not a part of Java Identifier. Enter the second input:A The character 'A' is a part of Java Identifier.
例子2
public class JavaCharcaterisJavaIdentifierPartExample_2 {
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 = '\u0013';
ch3 = '\u0765';
// Create three boolean primitives b1, b2 and b3.
boolean b1, b2,b3;
// Assign isJavaCharacterIdentifierPart results of ch1, ch2 and ch3 to b1, b2 and b3.
b1 = Character.isJavaIdentifierPart(ch1);
b2 = Character.isJavaIdentifierPart(ch2);
b3 = Character.isJavaIdentifierPart(ch3);
String str1 = "The first character is a part of java identifier: " + b1;
String str2 = "The second character is a part of java identifier:" + b2;
String str3 = "The third character is a part of java identifier: " + b3;
// Print the values of b1, b2 and b3.
System.out.println( str1 );
System.out.println( str2 );
System.out.println( str3 );
}
}
輸出:
The first character is a part of java identifier: false The second character is a part of java identifier:true The third character is a part of java identifier: true
Java 字符 isJavaIdentifierPart() 方法
Character 類的 isJavaIdentifierPart(int codePoint) 方法確定給定(或指定)字符是否是 Java 標識符的一部分。
如果以下任一條件為真,則給定字符是 Java 標識符的一部分:
- 字符是一個字母。
- 該字符是貨幣符號(如 '$')。
- 該字符為連接標點字符(如 '_')。
- 字符是一個數字。
- 字符為數字字母(如羅馬數字字符)。
- 字符是組合標記。
- 字符是非空格標記。
- isIdentifierIgnorable() 方法為特定字符返回真。
用法
public static boolean isJavaIdentifierPart(int codePoint)
參數
codePoint:codePoint 是需要測試的字符。
返回值
如果給定(或指定)字符是 Java 標識符的一部分,則 isJavaIdentifierPart(int codePoint) 方法返回一個布爾值 true。否則,該方法返回 false。
例子3
public class JavaChracterisJavaIdentifierPartExample3 {
public static void main(String[] args) {
// Initialize the codePoints.
int codepoint1 = 89;
int codepoint2 = 90;
int codepoint3 = 110;
// Check the results.
boolean check1 = Character.isJavaIdentifierPart(codepoint1);
boolean check2 = Character.isJavaIdentifierPart(codepoint2);
boolean check3 = Character.isJavaIdentifierPart(codepoint3);
// print result
if (check1) {
System.out.print("The first codepoint \'" + codepoint1 + "' is a part of Java identifier.\n");
} else {
System.out.print("The first codepoint \'" + codepoint1 + "' is not a part of Java identifier.\n");
}
if (check2) {
System.out.print("The second codepoint \'" + codepoint2 + "' is a part of Java identifier.\n");
} else {
System.out.print("The second codepoint \'" + codepoint2 + "' is not a part of Java identifier.\n");
}
if (check3) {
System.out.print("The third codepoint \'" + codepoint3 + "' is a part of Java identifier.\n");
} else {
System.out.print("The third codepoint \'" + codepoint3 + "' is not a part of Java identifier.\n");
}
}
}
輸出:
The first codepoint '89' is a part of Java identifier. The second codepoint '90' is a part of Java identifier. The third codepoint '110' is a part of Java identifier.
示例 4
public class JavaChracterisJavaIdentifierPartExample4 {
public static void main(String[] args) {
char cp1 = '_';
char cp2 = '5';
char cp3 = '9';
boolean b1 = Character.isJavaIdentifierPart(cp1);
boolean b2 = Character.isJavaIdentifierPart(cp2);
boolean b3 = Character.isJavaIdentifierPart(cp2);
System.out.println("The boolean value for the first codePoint is given as:"+ b1);
System.out.println("The boolean value for the second codePoint is given as:"+ b2);
System.out.println("The boolean value for the third codePoint is given as:"+ b3);
}
}
輸出:
The boolean value for the first codePoint is given as:true The boolean value for the second codePoint is given as:true The boolean value for the third codePoint is given as:true
例 5
public class JavaChracterisJavaIdentifierPartExample5 {
public static void main(String[] args) {
char cp1 = 'A';
char cp2 = 'G';
char cp3 = 'k';
boolean b1 = Character.isJavaIdentifierPart(cp1);
boolean b2 = Character.isJavaIdentifierPart(cp2);
boolean b3 = Character.isJavaIdentifierPart(cp2);
System.out.println("The boolean value for the first codePoint '" + cp1 + "' is given as:"+ b1);
System.out.println("The boolean value for the second codePoint '" + cp2 + "' is given as:"+ b2);
System.out.println("The boolean value for the third codePoint '" + cp3 + "' is given as:"+ b3);
}
}
輸出:
The boolean value for the first codePoint 'A' is given as:true The boolean value for the second codePoint 'G' is given as:true The boolean value for the third codePoint 'k' is given as:true
相關用法
- Java Character isJavaIdentifierstart()用法及代碼示例
- Java Character isJavaLetterOrDigit()用法及代碼示例
- 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 isUpperCase()用法及代碼示例
- Java Character isLowSurrogate()用法及代碼示例
- Java Character isWhitespace()用法及代碼示例
- Java Character isDefined()用法及代碼示例
- Java Character isHighSurrogate()用法及代碼示例
- Java Character isLetterOrDigit()用法及代碼示例
- Java Character isSpaceChar()用法及代碼示例
- Java Character codePointCount()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Character isJavaIdentifierPart() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。