Java - Character getType() 方法



描述

Java 的 Character getType() 方法用於獲取指示字元通用類別的值。眾所周知,每個程式碼點在 Unicode 中都分配有一個通用類別值。

例如,所有大寫字母都屬於 UPPERCASE 類別;類似地,所有小寫字母都屬於 LOWERCASE 類別。符號具有各種子類別,例如 CURRENCY_SYMBOLS、MATH_SYMBOL 等。

但是,此方法的一種多型形式(採用 char 值作為引數)不適用於補充字元。要獲取補充字元的通用類別的值,我們可以使用採用 codePoint 作為引數的多型形式。

語法

以下是 Java Character getType() 方法的語法

public static int getType(char ch)
(or)
public static int getType(int codePoint)

引數

  • ch − 要測試的字元

  • codePoint − 要測試的程式碼點

返回值

此方法返回一個表示字元通用類別的值。其返回型別為 int。

字元型別獲取示例

以下示例演示了 Java Character getType(char ch) 方法的用法。在此程式中,我們建立了兩個 char 變數併為它們分配了兩個值。然後使用 getType() 方法,檢索兩個 char 的型別並列印結果。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 character primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = 'M';
      ch2 = '$';

      // create 2 int primitives i1, i2
      int i1, i2;

      // assign getType values of ch1, ch2 to i1, i2
      i1 = Character.getType(ch1);
      i2 = Character.getType(ch2);

      /**
       *  value 1 represents UPPERCASE_LETTER
       *  value 26 represents CURRENCY_SYMBOL
      */

      String str1 = "Category of " + ch1 + " is " + i1;
      String str2 = "Category of " + ch2 + " is " + i2;

      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Category of M is 1 
Category of $ is 26

程式碼點型別獲取示例

以下示例演示了 Java Character getType(int codePoint) 方法的用法。在此程式中,我們建立了兩個 int 變數併為它們分配了兩個程式碼點。然後使用 getType() 方法,檢索兩個程式碼點的型別並列印結果。

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {
      int cp1, cp2;
      cp1 = 0x0034;
      cp2 = 0x006f;
      int i1, i2;
      i1 = Character.getType(cp1);
      i2 = Character.getType(cp2);

      /**
       *  value 9 represents DECIMAL_DIGIT_NUMBER
       *  value 2 represents LOWERCASE_LETTER
      */
      String str1 = "Category of cp1 is " + i1;
      String str2 = "Category of cp2 is " + i2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Category of cp1 is 9
Category of cp2 is 2

通用類別字元型別獲取示例

在下面的另一個示例中,我們將顯示 Unicode 系統中基於大小寫的字母的通用類別。該方法對大寫字母返回 1,對小寫字母返回 2。

package com.tutorialspoint;

public class getTypeDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      ch1 = 'a';
      ch2 = 'A';
      int i1, i2;
      i1 = Character.getType(ch1);
      i2 = Character.getType(ch2);
      System.out.println("Category of " + ch1 + " is " + i1);
      System.out.println("Category of " + ch2 + " is " + i2);
   }
}

輸出

如果我們編譯並執行上面的程式,則輸出將顯示為:

Category of a is 2
Category of A is 1

破折號字元型別獲取示例

符號中也有子類別。標點符號因其用法而異。讓我們使用此方法檢視兩種不同型別的破折號符號的類別。

package com.tutorialspoint;

public class getTypeDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      ch1 = '-';
      ch2 = '_';
      System.out.println("Category of " + ch1 + " is " + Character.getType(ch1));
      System.out.println("Category of " + ch2 + " is " + Character.getType(ch2));
   }
}

輸出

如果我們編譯程式碼並執行它,則將獲得以下輸出:

Category of - is 20
Category of _ is 23
java_lang_character.htm
廣告
© . All rights reserved.