Java String offsetByCodePoints() Method
Example
Get an index from a string which is offset from another index by a number of code points:
String myStr = "Hello, World!";
int result = myStr.offsetByCodePoints(3, 2);
System.out.println(result);
Definition and Usage
The offsetByCodePoints() method returns an index in a string which is offset from another index by a specified number of code points.
Note: A code point may be formed by more than one character. These code points will offset the index by more than 1.
Syntax
public int offsetByCodePoints(int index, int codePointOffset)
Parameter Values
| Parameter | Description |
|---|---|
| index | Required. Specifies the index in the string to measure the offset from. |
| codePointOffset | Required. Specifies the number of code points to offset by. Positive values will return a number greater than index and negative values will return a number less than index. |
Technical Details
| Returns: | An int representing the index which is codePointOffset code points away from index. |
|---|---|
| Throws: | IndexOutOfBoundsException - If index is negative or greater than the length of the string or if there are not enough code points in the string to offset by codePointOffset. |
| Java version: | 1.5 |
❮ String Methods