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


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