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


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


  1. Character.isJavaIdentifierPart(int codePoint)是Java中的一种内置方法,该方法确定指定字符是否可能是Java标识符的一部分,而不是第一个字符。
    如果满足以下任一条件,则字符可能是Java标识符的一部分:
    • 这是一封信
    • 它是货币符号(例如“ $”)
    • 它是连接的标点符号(例如“ _”)
    • 这是一个数字
    • 它是一个数字字母(例如罗马数字字符)
    • 这是一个组合标记
    • 这是一个非间距标记
    • isIdentifierIgnorable对字符返回true。

    用法:

    public static boolean isJavaIdentifierPart(int codePoint)

    参数:Integer数据类型的参数codePoint是指要测试的字符(Unicode代码点)。


    返回值:如果字符可能是Java标识符的一部分,则Character类的isJavaIdentifierPart(int codepoint)方法返回true。否则为假。

    下面的程序用于说明Character.isJavaIdentifierPart(int codepoint)方法:
    示例1:

    // Java program to illustrate 
    // Character.isJavaIdentifierPart() method 
    import java.lang.*; 
      
    public class gfg { 
      
        public static void main(String[] args) 
        { 
      
            // Create 2 int primitives c1, c2 
            int c1 = 0x01f2, c2 = 0x78c0; 
      
            // Assign isJavaIdentifierPart results of c1, c2 
            // to boolean primitives bool1, bool2 
            boolean bool1 = Character.isJavaIdentifierPart(c1); 
            boolean bool2 = Character.isJavaIdentifierPart(c2); 
      
            String str1 = "c1 may be part of a Java identifier is " + bool1; 
            String str2 = "c2 may be part of a Java identifier is " + bool2; 
      
            System.out.println(str1); 
            System.out.println(str2); 
        } 
    }
    输出:
    c1 may be part of a Java identifier is true
    c2 may be part of a Java identifier is true
    

    示例2:

    // Java program to illustrate 
    // Character.isJavaIdentifierPart(int codepoint) method 
    import java.lang.*; 
      
    public class gfg { 
      
        public static void main(String[] args) 
        { 
      
            // Create 2 int primitives c1, c2 
            int c1 = 0x85f1, c2 = 0x95c0; 
      
            // Assign isJavaIdentifierPart results of c1, c2 
            // to boolean primitives bool1, bool2 
            boolean bool1 = Character.isJavaIdentifierPart(c1); 
            boolean bool2 = Character.isJavaIdentifierPart(c2); 
      
            String str1 = "c1 may be part of a Java identifier is " + bool1; 
            String str2 = "c2 may be part of a Java identifier is " + bool2; 
      
            // Print b1, b2 values 
            System.out.println(str1); 
            System.out.println(str2); 
        } 
    }
    输出:
    c1 may be part of a Java identifier is true
    c2 may be part of a Java identifier is true
    
  2. Character.isJavaIdentifierPart(char ch)在所有方面都类似于先前的方法,但是不能处理补充字符。若要支持所有Unicode字符,包括补充字符,请使用前面的方法。

    用法:

    public static boolean isJavaIdentifierPart(char ch)

    参数:参数ch是字符数据类型,是指要测试的字符。

    返回值:如果字符可能是Java标识符的一部分,则Character类的isJavaIdentifierPart(char ch)方法返回true。否则为假。

    以下程序用于说明Character.isJavaIdentifierPart(char ch)方法的使用:
    示例1:

    // Java program to illustrate 
    // Character.isJavaIdentifierPart(char ch) method 
    import java.lang.*; 
      
    public class gfg { 
      
        public static void main(String[] args) 
        { 
      
            // Create 2 char primitives c1, c2 and assign values 
            char c1 = '5', c2 = '%'; 
      
            // Assign isJavaIdentifierPart results of  
            //c1, c2 to boolean primitives bool1, bool2 
            boolean bool1 = Character.isJavaIdentifierPart(c1); 
            boolean bool2 = Character.isJavaIdentifierPart(c2); 
      
            String str1 = c1 + " may be part of a Java identifier is " + bool1; 
            String str2 = c2 + " may be part of a Java identifier is " + bool2; 
      
            // Print bool1, bool2 values 
            System.out.println(str1); 
            System.out.println(str2); 
        } 
    }
    输出:
    5 may be part of a Java identifier is true
    % may be part of a Java identifier is false
    

    示例2:

    // Java program to illustrate 
    // Character.isJavaIdentifierPart(char ch) method 
    import java.lang.*; 
      
    public class gfg { 
      
        public static void main(String[] args) 
        { 
      
            // Create 2 char primitives c1, c2 and assign values 
            char c1 = '6', c2 = '*'; 
      
            // assign isJavaIdentifierPart results of  
            //c1, c2 to boolean primitives bool1, bool2 
            boolean bool1 = Character.isJavaIdentifierPart(c1); 
            boolean bool2 = Character.isJavaIdentifierPart(c2); 
      
            String str1 = c1 + " may be part of a Java identifier is " + bool1; 
            String str2 = c2 + " may be part of a Java identifier is " + bool2; 
      
            // Print bool1, bool2 values 
            System.out.println(str1); 
            System.out.println(str2); 
        } 
    }
    输出:
    6 may be part of a Java identifier is true
    * may be part of a Java identifier is false
    

参考: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#isUnicodeIdentifierPart(int)



相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 Character.isJavaIdentifierPart() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。