Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

Character Class ใน Java (java.lang.Character)

คลาส Character ใน package java.lang เป็น wrapper class สำหรับ primitive type char ใช้ห่อหุ้มค่า char เดียวในออบเจ็กต์ และให้ utility methods มากมายสำหรับจัดการตัวอักษร

Field สำคัญ

Field คำอธิบาย
static int BYTES จำนวน bytes ใช้เก็บ char (2 bytes = 16 bits)
static byte MAX_RADIX Radix สูงสุดสำหรับการแปลงฐาน (36)
static char MAX_VALUE ค่าสูงสุดของ char: \\uFFFF (65,535)
static char MIN_VALUE ค่าต่ำสุดของ char: \\u0000 (0)
static int SIZE จำนวน bits: 16
static Class<Character> TYPE Class object สำหรับ primitive char

เมธอดสำคัญ (Static Methods)

เมธอด คำอธิบาย
static int charCount(int codePoint) จำนวน char values ที่ต้องใช้ represent code point (1 หรือ 2 สำหรับ supplementary chars)
static boolean isLetter(char ch) ตรวจสอบว่าเป็นตัวอักษรหรือไม่
static boolean isDigit(char ch) ตรวจสอบว่าเป็นตัวเลข 0-9 หรือไม่
static boolean isLetterOrDigit(char ch) ตรวจสอบว่าเป็นตัวอักษรหรือตัวเลข
static boolean isLowerCase(char ch) ตรวจสอบว่าเป็นตัวพิมพ์เล็กหรือไม่
static boolean isUpperCase(char ch) ตรวจสอบว่าเป็นตัวพิมพ์ใหญ่หรือไม่
static boolean isWhitespace(char ch) ตรวจสอบว่าเป็น whitespace หรือไม่
static boolean isSpaceChar(char ch) ตรวจสอบว่าเป็น Unicode space character หรือไม่
static char toLowerCase(char ch) แปลงเป็นตัวพิมพ์เล็ก
static char toUpperCase(char ch) แปลงเป็นตัวพิมพ์ใหญ่
static char toTitleCase(char ch) แปลงเป็น title case
static int digit(char ch, int radix) คืนค่าตัวเลขของ char ใน radix ที่กำหนด
static char forDigit(int digit, int radix) คืนค่า char represent digit ใน radix
static int getNumericValue(char ch) ค่าตัวเลขของ Unicode character
static boolean isDefined(char ch) ตรวจสอบว่า char มี definition ใน Unicode
static int codePointAt(CharSequence seq, int index) ดึง Unicode code point ที่ index
static int codePointBefore(CharSequence seq, int index) ดึง code point ก่อน index
static int codePointCount(CharSequence seq, int begin, int end) นับ code points ในช่วง
static int offsetByCodePoints(CharSequence seq, int index, int codePointOffset) หา index offset โดย code points
static String toString(char c) แปลง char เป็น String

Instance Methods

เมธอด คำอธิบาย
char charValue() คืนค่า primitive char
int compareTo(Character another) เปรียบเทียบค่า char
boolean equals(Object obj) เปรียบเทียบ object
int hashCode() Hash code
String toString() แปลงเป็น String

ตัวอย่างการใช้งาน

public class CharacterDemo {
    public static void main(String[] args) {
        // Static methods
        System.out.println("isLetter('0'): " + Character.isLetter('0')); // false
        System.out.println("isLetter('h'): " + Character.isLetter('h')); // true
        System.out.println("isLetter('K'): " + Character.isLetter('K')); // true
        System.out.println("isDigit('5'): " + Character.isDigit('5'));   // true
        System.out.println("isLowerCase('h'): " + Character.isLowerCase('h')); // true
        System.out.println("isUpperCase('K'): " + Character.isUpperCase('K')); // true
        System.out.println("toUpperCase('h'): " + Character.toUpperCase('h')); // H
        System.out.println("toLowerCase('K'): " + Character.toLowerCase('K')); // k
        System.out.println("toString('0'): " + Character.toString('0')); // "0"
        System.out.println("toString('H'): " + Character.toString('H')); // "H"

        // Unicode / Supplementary characters
        String emoji = "😀"; // U+1F600 - supplementary character
        System.out.println("Emoji length (chars): " + emoji.length()); // 2 (surrogate pair)
        System.out.println("Emoji code points: " + emoji.codePointCount(0, emoji.length())); // 1
        System.out.println("charCount for 😀: " + Character.charCount(emoji.codePointAt(0))); // 2

        // Digit in different radix
        System.out.println("digit('F', 16): " + Character.digit('F', 16)); // 15
        System.out.println("forDigit(15, 16): " + Character.forDigit(15, 16)); // f

        // Wrapper object
        Character ch = 'A';
        System.out.println("charValue: " + ch.charValue()); // A
        System.out.println("hashCode: " + ch.hashCode());   // 65
    }
}

Output

isLetter('0'): false
isLetter('h'): true
isLetter('K'): true
isDigit('5'): true
isLowerCase('h'): true
isUpperCase('K'): true
toUpperCase('h'): H
toLowerCase('K'): k
toString('0'): 0
toString('H'): H
Emoji length (chars): 2
Emoji code points: 1
charCount for 😀: 2
digit('F', 16): 15
forDigit(15, 16): f
charValue: A
hashCode: 65

Unicode Support (สำคัญ)

Java char เป็น UTF-16 code unit (16 bits) ไม่ใช่ Unicode code point ทั้งหมด

  • BMP characters (U+0000 - U+FFFF): 1 char = 1 code point
  • Supplementary characters (U+10000 - U+10FFFF): 2 chars (surrogate pair) = 1 code point

ใช้เมธอด codePointAt, codePointCount, offsetByCodePoints สำหรับจัดการ Unicode อย่างถูกต้อง

ตัวอย่าง Supplementary Character Handling

String str = "A😀B"; // A + 😀 (surrogate pair) + B = 4 chars, 3 code points

// ผิด: ใช้ char length
for (int i = 0; i < str.length(); i++) {
    System.out.println(str.charAt(i)); // พิมพ์ surrogate แยกกัน
}

// ถูก: ใช้ code points
str.codePoints().forEach(cp -> System.out.println((char)cp)); // หรือใช้ Character.toChars(cp)

// หรือ
for (int i = 0; i < str.length(); ) {
    int cp = str.codePointAt(i);
    System.out.println(Character.toChars(cp));
    i += Character.charCount(cp);
}