Java中的字符getType() 方法及示例
- 字符.getType(char ch)是Java中的一个内置方法,它返回一个指示字符的一般类别的值。此方法无法处理补充字符。要支持所有 Unicode字符,包括补充字符,请使用 getType(int) 方法。
句法:
public static int getType(char ch)
参数:该方法接受一个字符数据类型的参数ch ,它指的是要测试的字符。
返回值:此方法返回一个整数类型的值,表示字符的一般类别。
下面的程序说明了字符.getType(char ch) 方法的使用:
方案一: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
方案二:
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
- Java.lang。 字符 getType(int codePoint)与前面的方法在所有方面都相似,该方法可以处理补充字符。
句法:
public static int getType(int codePoint)
参数:该方法接受整数数据类型的单个参数codePoint并引用要测试的字符(Unicode 代码点)。
返回值:此方法返回一个int类型的值,表示字符的一般类别。
下面的程序演示了上述方法:
方案一:// 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
方案二:
// 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
参考: https: Java/lang/ 字符.html#getType(char)