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


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