當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Character isJavaIdentifierstart()用法及代碼示例


Character 類的 isJavaIdentifierStart(char ch) 方法確定給定(或指定)的字符是否被視為 Java 標識符中的第一個字符。

當且僅當以下任一條件為真時,字符才可以作為 Java 標識符的開頭:

  • isLetter(ch) 返回真。
  • getType(ch) 返回 LETTER_NUMBER。
  • 字符 ch 是貨幣符號。
  • 字符 ch 是連接標點符號,例如 '_'。

注意:上述方法不能用於處理增補字符。為了支持所有的 Unicode 字符,包括所有的增補字符,我們可以使用 isJavaIdentifierStart(int) 方法。

用法

public static boolean isJavaIdentifierStart(char ch)

參數

ch: 這是需要測試的字符。

返回值

isJavaIdentifierStart(char ch) 方法返回一個布爾值,即如果給定(或指定)字符可以作為特定 Java 標識符的開頭,則返回 true。否則,該方法返回 false。

例子1

public class JavaChracterisJavaIdentifierStartExample1 {
    public static void main(String[] args) {
      // Create three char primitives ch1, ch2 and ch3.
         char ch1, ch2, ch3;
      // Assign values to ch1, ch2 and ch3.
         ch1 = '1';
         ch2 = '5';
         ch3 = '_';
      // Create three boolean primitives b1, b2 and b3.
      boolean b1, b2, b3;
      // Assign result to b1, b2 and b3.
      b1 = Character.isJavaIdentifierStart(ch1);
      b2 = Character.isJavaIdentifierStart(ch2);
      b3 = Character.isJavaIdentifierStart(ch3);
      String str1 = " The first character may start a Java identifier:" + b1;
      String str2 = " The second character may start a Java identifier:" + b2;
      String str3 = " The third character may start a Java identifier:" + b3;
      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
   }
}

輸出:

The first character may start a Java identifier:false
 The second character may start a Java identifier:false
 The third character may start a Java identifier:true

例子2

public class JavaChracterisJavaIdentifierStartExample2 {
    public static void main(String[] args) {
      // Create three char primitives ch1, ch2 and ch3.
         char ch1, ch2, ch3;
      // Assign values to ch1, ch2 and ch3.
         ch1 = 'A';
         ch2 = 'b';
         ch3 = 'G';
      // Create three boolean primitives b1, b2 and b3.
      boolean b1, b2, b3;
      // Assign result to b1, b2 and b3.
      b1 = Character.isJavaIdentifierStart(ch1);
      b2 = Character.isJavaIdentifierStart(ch2);
      b3 = Character.isJavaIdentifierStart(ch3);
      String str1 = " The first character may start a Java identifier:" + b1;
      String str2 = " The second character may start a Java identifier:" + b2;
      String str3 = " The third character may start a Java identifier:" + b3;
      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
   }
}

輸出:

The first character may start a Java identifier:true
The second character may start a Java identifier:true
The third character may start a Java identifier:true

例子3

import java.util.Scanner;
public class JavaChracterisJavaIdentifierStartExample3 {
    public static void main(String[] args) {
       // Ask the user to enter ther input.
	  System.out.print("Enter the first character:");
      // Use the Scanner class to get the user input
	 Scanner s1 = new Scanner(System.in);
      // Get a single character.
	 char value1 = s1.nextLine().toCharArray()[0];
      // Check the user input. 
	 boolean check1 = Character.isJavaIdentifierStart(value1);
     // print result
	 if (check1) {
System.out.print("The first input \'" + value1 + "\' may start a Java identifier.\n");
	} else {
System.out.print("The first input \'" + value1 + "\' may not start a Java identifier.\n");
		}
         System.out.print("Enter the second character:");
	 Scanner s2 = new Scanner(System.in);
	 char value2 = s2.nextLine().toCharArray()[0];
	 boolean check2 = Character.isJavaIdentifierStart(value1);
   
	 if (check2) {
System.out.print("The second input \'" + value2 + "\' may start a Java identifier.\n");
	} else {
System.out.print("The second input \'" + value2 + "\' may not start a Java identifier.\n");
		}
	}
}

輸出:

Enter the first character:)
The first input ')' may not start a Java identifier.
Enter the second character:k
The second input 'k' may not start a Java identifier.




相關用法


注:本文由純淨天空篩選整理自 Java Character isJavaIdentifierstart() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。