如何在 Java 中找到給定字元的 unicode 類別?
字元類是物件的一個子類,它將基本型別 char 的值包裝在一個物件中。Character 型別的物件包含一個欄位,其型別為 char。
我們可以使用 getType() 方法來確定特定字元的 unicode 類別。這是 Character 類的一個靜態方法,它返回一個 char ch 的整數值,表示 unicode 中的一般類別。
語法
public static int getType(char ch)
示例
public class CharacterTypeTest { public static void main(String args[]) { System.out.println("T represnts unicode category of: " + Character.getType('T')); System.out.println("@ represnts unicode category of: " + Character.getType('@')); System.out.println(") represnts unicode category of: " + Character.getType(')')); System.out.println("1 represnts unicode category of: " + Character.getType('1')); System.out.println("- represnts unicode category of: " + Character.getType('-')); System.out.println("_ represnts unicode category of: " + Character.getType('_')); System.out.println("a represnts unicode category of: " + Character.getType('a')); } }
輸出
T represnts unicode category of: 1 @ represnts unicode category of: 24 ) represnts unicode category of: 22 1 represnts unicode category of: 9 - represnts unicode category of: 20 _ represnts unicode category of: 23 a represnts unicode category of: 2
廣告