java.lang.Character.charCount(int codePoint) 確定表示指定字元所需的字元的值的數目(Unicode程式碼點)。如果指定的字元是等於或大於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.yiibai; 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