Character.charCount()方法确定表示指定字符(Unicode代码点)所需的char值的数量。如果指定的字符等于或大于0x10000,则该方法返回2。否则,该方法返回1。
现在让我们看一个例子-
import java.lang.*;
public class Demo {
public static void main(String[] args) {
//创建并将值分配给int代码点cp-
int cp = 0x12345;
//创建一个int res-
int res;
//将cp上charCount的结果分配给res-
res = Character.charCount(cp);
String str1 = "It is not a valid supplementary character";
String str2 = "It is a valid supplementary character";
//打印res值
if ( res == 1 ) {
System.out.println( str1 );
} else if ( res == 2 ) {
System.out.println( str2 );
}
}
}输出结果
It is a valid supplementary character
让我们看另一个例子-
import java.lang.*;
public class Demo {
public static void main(String[] args) {
int c = 0x11987;
int res = Character.charCount(c);
System.out.println(res);
}
}输出结果
2