Java Character getType()方法及示例
- Character.getType(char ch)是java中的一个内置方法,它返回一个表示字符的一般类别的值。这个方法不能处理补充字符。要支持所有Unicode字符,包括补充字符,请使用getType(int)方法。
语法:
public static int getType(char ch)
参数:该方法接受一个字符数据类型的参数ch,它指的是要测试的字符。
返回值:该方法返回一个整数类型的值,代表该字符的一般类别。
下面的程序说明了Character.getType(char ch)方法的使用。
程序 1:
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
程序 2:
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.Character getType(int codePoint)**在所有方式上与前一个方法相似,这个方法可以处理补充字符。
语法:
公共静态int getType(int codePoint)
参数:该方法接受一个整数数据类型的参数codePoint,指的是要测试的字符(Unicode码位)。
返回值:该方法返回一个int类型的值,代表该字符的一般类别。
下面的程序演示了上述的方法。
程序 1:
// 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
程序 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://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#getType(char)