Java 字符串 offsetByCodePoints() 方法
示例
从字符串中获取相对于另一个索引偏移若干个代码点的索引:
String myStr = "Hello, World!";
int result = myStr.offsetByCodePoints(3, 2);
System.out.println(result);
定义和用法
offsetByCodePoints() 方法返回字符串中相对于另一个索引偏移指定数量的代码点的索引。
注意:一个代码点可能由多个字符组成。这些代码点会使索引偏移超过 1 个字符。
语法
public int offsetByCodePoints(int index, int codePointOffset)
参数值
| 参数 | 描述 |
|---|---|
| index | 必需。指定字符串中要测量偏移量的索引。 |
| codePointOffset | 必需。指定要偏移的代码点数。正值将返回大于 index 的数字,负值将返回小于 index 的数字。 |
技术细节
| 返回值: | 一个 int,表示距离 index 代码点数 codePointOffset 个代码点的索引。 |
|---|---|
| 抛出异常: | IndexOutOfBoundsException - 如果 index 为负数或大于字符串长度,或者字符串中没有足够的代码点数来偏移 codePointOffset 个代码点。 |
| Java 版本: | 1.5 |
❮ String 字符串方法

