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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。