Java StringBuilder offsetByCodePoints() Method
Example
Find the index that is 3 code points after index 0:
StringBuilder sb = new StringBuilder("Hello");
int result = sb.offsetByCodePoints(0, 3);
System.out.println(result);
Definition and Usage
The offsetByCodePoints() method returns the index that is a number of Unicode values (code points) away from a given index.
For most text, the result is the same as index + codePointOffset. It can be different for special characters, like some emoji, that use two char values.
Syntax
public int offsetByCodePoints(int index, int codePointOffset)
Parameter Values
| Parameter | Description |
|---|---|
| index | Required. The index to start from. |
| codePointOffset | Required. The number of code points to move. A negative number moves backward. |
Technical Details
| Returns: | An int value, representing the new index. |
|---|---|
| Throws: | IndexOutOfBoundsException - if index is negative or larger than the length, or if there are not enough code points to move. |
Related Pages
Java String offsetByCodePoints() Method
❮ StringBuilder Methods