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


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


  1. Character.getType(char ch)是Java中的一種內置方法,它返回一個指示字符的常規類別的值。此方法不能處理補充字符。要支持所有Unicode字符(包括補充字符),請使用getType(int)方法。

    用法:

    public static int getType(char ch)

    參數:該方法接受字符數據類型的一個參數ch,該參數ch是要測試的字符。


    返回值:此方法返回一個整數類型的值,表示字符的常規類別。

    以下示例程序旨在說明Character.getType(char ch)方法的使用:
    示例1:

    import java.lang.*; 
      
    public class gfg { 
      
       public static void main(String[] args) { 
      
      
          // Create 2 character primitives ch1, ch2 and assigning values 
          char c1 = 'K', c2 = '%'; 
      
          // Assign getType values of c1, c2 to int primitives int1, int2 
          int int1 = Character.getType(c1); 
          int int2 = Character.getType(c2); 
      
          String str1 = "Category of " + c1 + " is " + int1; 
          String str2 = "Category of " + c2 + " is " + int2; 
      
          System.out.println( str1 ); 
          System.out.println( str2 ); 
       } 
    }
    輸出:
    Category of K is 1
    Category of % is 24
    

    示例2:

    import java.lang.*; 
      
    public class gfg { 
      
       public static void main(String[] args) { 
      
      
          // Create 2 character primitives ch1, ch2 and assigning values 
          char c1 = 'T', c2 = '^'; 
      
          // Assign getType values of c1, c2 to inyt primitives int1, int2 
         int int1 = Character.getType(c1); 
         int int2 = Character.getType(c2); 
      
          String str1 = "Category of " + c1 + " is " + int1; 
          String str2 = "Category of " + c2 + " is " + int2; 
      
          System.out.println(str1); 
          System.out.println(str2); 
       } 
    }
    輸出:
    Category of T is 1
    Category of ^ is 27
    
  2. java.lang.Character getType(int codePoint)在所有方麵都類似於先前的方法,該方法可以處理補充字符。

    用法:

    public static int getType(int codePoint)
    

    參數:該方法接受整數數據類型的單個參數codePoint,表示要測試的字符(Unicode代碼點)。

    返回值:此方法返回一個int類型的值,表示字符的常規類別。

    下麵的程序演示了上述方法:
    示例1:

    // Java program to demonstrate 
    // the above method 
    import java.lang.*; 
      
    public class gfg { 
      
       public static void main(String[] args) { 
      
          // int primitives c1, c2 
          int c1 = 0x0037, c2 = 0x016f; 
      
      
          // Assign getType values of c1, c2 to int primitives int1, int2 
         int int1 = Character.getType(c1); 
         int int2 = Character.getType(c2); 
      
          // Print int1, int2 values 
          System.out.println(  "Category of c1 is " + int1); 
          System.out.println(  "Category of c1 is " + int2); 
       } 
    }
    輸出:
    Category of c1 is 9
    Category of c1 is 2
    

    示例2:

    // Java program to demonstrate 
    // the above method 
    import java.lang.*; 
      
    public class gfg { 
      
       public static void main(String[] args) { 
      
          // int primitives c1, c2 
          int c1 = 0x0135, c2 = 0x015f; 
      
      
          // Assign getType values of c1, c2 to int primitives int1, int2 
         int int1 = Character.getType(c1); 
         int int2 = Character.getType(c2); 
      
          // Print int1, int2 values 
          System.out.println(  "Category of c1 is " + int1); 
          System.out.println(  "Category of c1 is " + int2); 
       } 
    }
    輸出:
    Category of c1 is 2
    Category of c1 is 2
    
  3. 參考: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#getType(char)



相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 Character getType() Method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。