当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。