| 方法 | 用途 | 示例 |
isValidCodePoint(int cp) | 判斷有效代碼點 (≤ 0x10FFFF) | isValidCodePoint(0x10FFFF) → true |
isBmpCodePoint(int cp) | 判斷 BMP 字符 (≤ 0xFFFF) | isBmpCodePoint('中') → true |
isSupplementaryCodePoint(int cp) | 判斷輔助字符 (0x10000~0x10FFFF) | isSupplementaryCodePoint(0x1F600) → true (😀) |
isHighSurrogate(char ch) | 判斷高代理項 | isHighSurrogate('\uD83D') → true |
isLowSurrogate(char ch) | 判斷低代理項 | isLowSurrogate('\uDE00') → true |
isSurrogatePair(char high, char low) | 判斷是否合法代理對 | isSurrogatePair('\uD83D', '\uDE00') → true |
charCount(int cp) | 代碼點佔用 char 數 (1 或 2) | charCount(0x1F600) → 2 |
toCodePoint(char high, char low) | 代理對 → 代碼點 | toCodePoint('\uD83D', '\uDE00') → 0x1F600 |
toChars(int cp) | 代碼點 → char[] (長度 1 或 2) | toChars(0x1F600) → ['\uD83D', '\uDE00'] |
highSurrogate(int cp) / lowSurrogate(int cp) | 輔助字符拆分高/低代理 | highSurrogate(0x1F600) → '\uD83D' |
| 方法 | 說明 |
codePointCount(CharSequence seq, int begin, int end) | 區間內代碼點數量 (輔助字符計 1) |
codePointAt(CharSequence seq, int index) | 索引處代碼點 (自動合併代理對) |
codePointBefore(CharSequence seq, int index) | 索引前一個代碼點 |
offsetByCodePoints(CharSequence seq, int index, int cpOffset) | 按代碼點偏移計算 char 索引 |
示例:"中😀".codePointCount(0, 3) → 2 (char 長度 3,代碼點 2)。
字符屬性檢查 (基於 Unicode General Category)
| 方法 | 對應 Category / 語義 | 典型字符 |
getType(int cp) | 返回 Category 常量 (UPPERCASE_LETTER=1, LOWERCASE_LETTER=2, DECIMAL_DIGIT_NUMBER=9, SPACE_SEPARATOR=12...) | |
isDefined(int cp) | Category != 0 (已分配字符) | |
isDigit(int cp) | Category == DECIMAL_DIGIT_NUMBER | '9', '9' (全角) |
isLetter(int cp) | Letter 類 (UPPER/LOWER/TITLE/MODIFIER/OTHER) | 'A', '中' |
isLetterOrDigit(int cp) | isLetter || isDigit | |
isAlphabetic(int cp) | isLetter || Category == LETTER_NUMBER (如羅馬數字 Ⅰ) | 'Ⅰ' |
isSpaceChar(int cp) | SPACE/LINE/PARAGRAPH_SEPARATOR | ' ', '\n' |
isWhitespace(int cp) | 更寬鬆:含控制字符 \t \n \r、全角空格 | '\t', ' ' |
isLowerCase / isUpperCase | Category == LOWER/UPPERCASE_LETTER | 'a' / 'A' |
isIdeographic(int cp) | 表意文字 (漢字、日韓漢字) | '中', '日' |
isISOControl(int cp) | ISO-8859-1 控制字符 (0-31, 127-159) | '\u0000' |
isJavaIdentifierStart(int cp) | 字母/下劃線/$ (可作標識符首字符) | '_', '$', '中' |
isJavaIdentifierPart(int cp) | Start + 數字 (可作標識符非首字符) | '1', '中' |
isMirrored(int cp) | 鏡像字符 (括號、尖括號) | '(', ')', '<', '>' |
字符轉換與數值
| 方法 | 說明 | 示例 |
toLowerCase(int cp) / toUpperCase(int cp) | 大小寫轉換 (返回代碼點) | toLowerCase('A') → 97 ('a') |
getNumericValue(int cp) | 字符數值 (0-9 → 0-9; 字母 → 10-35) | getNumericValue('F') → 15 |
digit(int cp, int radix) | 指定進制下數值 (無效返回 -1) | digit('F', 16) → 15; digit('G',16) → -1 |
forDigit(int digit, int radix) | 數值 → 字符 (無效返回 '\0') | forDigit(15, 16) → 'f' |
reverseBytes(char ch) | 字節序反轉 | reverseBytes(0x1234) → 0x3412 |
實戰技巧
- 正確遍歷字符串:
for (int i=0; i 避免輔助字符拆分錯誤。
- 判斷是否為漢字:
Character.isIdeographic(cp) 或範圍 0x4E00~0x9FFF (基本漢字) + 擴展區。
- 解析十六進制字符:
Character.digit(ch, 16) 統一處理 0-9/a-f/A-F/全角。
- IDN/域名驗證:結合
isJavaIdentifierStart/Part、isDigit、連字符檢查。
總結
Character 類封裝了 Unicode 字符級操作,屏蔽了 UTF-16 char 與代碼點的複雜映射。掌握代碼點/輔助字符概念、核心檢查/轉換/屬性方法,能正確處理國際化文本、Emoji、生僻字等現代應用場景,是 Java 文本處理的基石。
- 上一篇文章: Java 集合框架架構圖解:Collection/Map 體系、核心實現類與關係一覽
- 下一篇文章: Java Vector 與 Enumeration 詳解:唯讀舊集合、枚舉遍歷與現代替代方案
Copyright ©
程式碼之家 All Rights Reserved