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


Java Character isJavaIdentifierPart()用法及代码示例


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