1. 方法签名
codePointCount()
方法用于计算指定文本范围内的 Unicode 码点数量。文本范围从第一个索引开始,到第二个索引前一位结束(即 [beginIndex, endIndex-1])。
public int codePointCount(int beginIndex, int endIndex)
2. 基本用法
该方法直接返回指定范围内的码点数量,特别适合处理包含代理对(Surrogate Pairs)的字符(如 Emoji)。
@Test
public void whenCallCodePointCount_thenCorrect() {
assertEquals(2, "abcd".codePointCount(0, 2)); // 前两个字符 'a' 和 'b'
assertEquals(1, "👨👩👧👦".codePointCount(0, 11)); // 复合 Emoji 被视为 1 个码点
}
3. 异常处理
调用时需注意索引范围,否则会抛出 IndexOutOfBoundsException
。触发条件包括:
beginIndex
为负数beginIndex
大于endIndex
endIndex
超出字符串长度
@Test(expected = IndexOutOfBoundsException.class)
public void whenSecondIndexEqualToLengthOfString_thenExceptionThrown() {
char character = "Paul".charAt(4); // 索引 4 等于字符串长度(合法范围 0-3)
}
// 更符合主题的异常示例
@Test(expected = IndexOutOfBoundsException.class)
public void whenInvalidRange_thenExceptionThrown() {
"hello".codePointCount(3, 2); // beginIndex > endIndex
}
⚠️ 踩坑提示:
- 字符串长度
length()
返回的是char
数量,而非码点数量 - 处理国际化文本时,优先使用码点相关方法(如
codePointAt()
)避免字符截断问题