Java.lang.Character.charCount() 方法
描述
java.lang.Character.charCount(int codePoint) 确定表示指定字符(Unicode 代码点)所需的 char 值的数量。 如果指定字符等于或大于0x10000,则该方法返回2。否则,该方法返回1。
此方法不会验证指定字符是否为有效的 Unicode 代码点。 如有必要,调用者必须使用 isValidCodePoint 验证字符值。
声明
以下是 java.lang.Character.charCount() 方法的声明。
public static int charCount(int codePoint)
参数
codePoint − 要测试的字符(Unicode 代码点)
返回值
如果字符是有效的补充字符,此方法返回 2,否则返回 1。
异常
NA
示例
下面的例子展示了 lang.Character.charCount() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
public static void main(String[] args) {
// create and assign values to int codepoint cp
int cp = 0x12345;
// create an int res
int res;
// assign the result of charCount on cp to res
res = Character.charCount(cp);
String str1 = "It is not a valid supplementary character";
String str2 = "It is a valid supplementary character";
// print res value
if ( res == 1 ) {
System.out.println( str1 );
} else if ( res == 2 ) {
System.out.println( str2 );
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
It is a valid supplementary character

