Character.charCount() วิธีการกำหนดจำนวนของค่าถ่านที่จำเป็นเพื่อแสดงอักขระที่ระบุ (จุดรหัส Unicode) หากอักขระที่ระบุมีค่าเท่ากับหรือมากกว่า 0x10000 เมธอดจะคืนค่า 2 มิฉะนั้น เมธอดจะคืนค่า 1
ตัวอย่าง
เรามาดูตัวอย่างกัน −
import java.lang.*;
public class Demo {
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
ตัวอย่าง
เรามาดูตัวอย่างกัน −
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