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


Java Character getType(char ch)用法及代码示例

Character 类的 getType(char ch) 方法返回一个值,该值指示字符的一般类别。此方法不用于补充字符。我们可以使用 getType(int codePoint) 方法来支持包括增补在内的所有 Unicode 字符。

用法

public static int getType(char ch)

参数

ch:需要测试的字符。

返回值

此方法返回一个 int 类型的值,表示字符的一般类别。

例子1

public class JavaCharactergetTypeExample1 {
     public static void main(String args[])
     {
      System.out.println("The integer value for the character 2 is given as:");  	 
      System.out.println( Character.getType('2'));
      System.out.println("The integer value for the character { is given as:");  	
      System.out.println( Character.getType('{'));  	  
      System.out.println("The integer value for the character * is given as:");  	
      System.out.println( Character.getType('*')); 
 	  System.out.println("The integer value for the character @ is given as:");  	
      System.out.println( Character.getType('@'));     
      System.out.println("The integer value for the character $ is given as:");  	
      System.out.println( Character.getType('$'));  
     }
    }

输出:

The integer value for the character 2 is given as:
9
The integer value for the character { is given as:
21
The integer value for the character * is given as:
24
The integer value for the character @ is given as:
24
The integer value for the character $ is given as:
26

例子2

import java.util.Scanner;
  public class JavaCharactergetTypeExample2 {
    public static void main(String[] args) {
      // Ask the user for the input;
 System.out.print("Enter the first input of your choice:");
       // Use Scanner to get the input.
    Scanner obj1 = new Scanner(System.in);
    char[] value1 = obj1.nextLine().toCharArray();
    // Get a value which indicates the general category of the character.
    for (char ch1:value1) {
    int result1 = Character.getType(ch1);			
System.out.println("The general category for the character " + ch1 + " is given as:" + result1);	
            
System.out.print("Enter the second input of your choice:");
    Scanner obj2 = new Scanner(System.in);
    char[] value2 = obj2.nextLine().toCharArray();
    for (char ch2:value2) {
       int result2 = Character.getType(ch2);			
System.out.println("The general category for the character " + ch2 + " is given as:" + result2);		
           }
        }
     }
  }

输出:

Enter the first input of your choice:{
The general category for the character { is given as:21
Enter the second input of your choice:&
The general category for the character & is given as:24

例子3

public class JavaCharactergetTypeExample3 {
    public static void main(String[] args) {
      // Create two character primitives:ch1 and ch2
          char ch1, ch2, ch3;
      // Assign the values to ch1, ch2 and ch3.
          ch1 = '1';
          ch2 = '!';
          ch3 = ')';
      // Create three int primitives:obj1, obj2
          int obj1, obj2,obj3;

       // Assign the getType values of ch1 and ch2 to i1 and i2 respectively.
          obj1 = Character.getType(ch1);
          obj2 = Character.getType(ch2);
          obj3 = Character.getType(ch3);
          String str1 = "The general category of character " + ch1 + " is:" + obj1;
          String str2 = "The general category of character " + ch2 + " is:" + obj2;
          String str3 = "The general category of character " + ch3 + " is:" + obj3;
          System.out.println( str1 );
          System.out.println( str2 );
          System.out.println( str3 );
       }
    }

输出:

The general category of character 1 is:9
The general category of character ! is:24
The general category of character ) is:22

Java 字符 getType(int codePoint) 方法

Character 类的 getType(int codePoint) 方法一般返回表示字符一般类别的值。

用法

public static int getType(int codePoint)

参数

codePoint:需要测试的字符(Unicode codePoint)。

返回值

getType(int codePoint) 方法返回表示字符一般类别的整数类型值。

示例 4

public class JavaCharactergetType_Example1 {
    public static void main(String[] args) {
        //Iinitialize two codepoint.
        int cp1 = 88;
        int cp2 = 110;
        // Convert the codepoints into char.
        char ch1 = (char) cp1;
        char ch2 = (char) cp2;
        // Get the result.
        int result1 = Character.getType(cp1);
        int result2 = Character.getType(cp2);
        // Print the result
        System.err.println("The general category for the character " + ch1 + " is given as:" + result1);
        System.err.println("The general category for the character " + ch2 + " is given as:" + result2);
    }
}

输出:

The general category for the character X is given as:1
The general category for the character n is given as:2

例 5

public class JavaCharactergetType_Example2 {
    public static void main(String[] args) {	 
         // Create two int primitives:cp1 and cp2
         int cp1, cp2;
     // Assign the values to cp1 and cp2 respectively.
         cp1 = 34;
         cp2 = 100;
       // create two int primitives i1 and i2
         int i1, i2;
    // Assign THE getType values of cp1 and cp2 to i1 and i2 respectively.
         i1 = Character.getType(cp1);
         i2 = Character.getType(cp2);	      
String str1 = "The general category of the first character"+ " is given as:"+ i1;
String str2 = "The general category of the second character is given as:"+ i2;
     // Print the values of i1 and i2.
         System.err.println( str1 );
         System.err.println( str2 );
           }
        }

输出:

The general category of the first character is given as:24
The general category of the second character is given as:2



相关用法


注:本文由纯净天空筛选整理自 Java Character getType(char ch) Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。