|
| 1 | +--- |
| 2 | +Title: '.offsetByCodePoints()' |
| 3 | +Description: 'Returns the new index of a character in a string after applying the specified code point offset.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | +Tags: |
| 7 | + - 'Strings' |
| 8 | + - 'Methods' |
| 9 | +CatalogContent: |
| 10 | + - 'learn-java' |
| 11 | + - 'paths/computer-science' |
| 12 | +--- |
| 13 | + |
| 14 | +The **`offsetByCodePoints()`** function is used to calculate the index of a character in a string by specifying a starting index and a code point offset. It returns the new index after applying the offset limit. |
| 15 | + |
| 16 | +It is useful when working with strings that contain characters with multiple code points, such as emojis or characters from non-Latin scripts. This function helps accurately navigate and manipulate the index based on code points, allowing you to access specific characters or portions of the string. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +Strings.offsetByCodePoints(startIndex, offsetIndex) |
| 22 | +``` |
| 23 | + |
| 24 | +- `startIndex` (int): The starting index in the string from which the offset is applied. |
| 25 | +- `offsetIndex` (int): The offset, in terms of code points, by which the index is adjusted. Positive values move the index forward, and negative values move it backward. |
| 26 | + |
| 27 | +## Example 1 |
| 28 | + |
| 29 | +In this example, the code point offset is set to `7`, which means the new index will be `7`. This results in the new index pointing to the character `'W'` in the string. |
| 30 | + |
| 31 | +```java |
| 32 | +class OffsetByCodePoints { |
| 33 | + public static void main(String[] args) { |
| 34 | + String str = "Hello, World!"; |
| 35 | + int startIndex = 0; |
| 36 | + int offsetIndex = 7; |
| 37 | + |
| 38 | + int newIndex = str.offsetByCodePoints(startIndex, offsetIndex); |
| 39 | + System.out.println("New Index: " + newIndex); |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +The code above results in the following output: |
| 45 | + |
| 46 | +``` |
| 47 | +New Index: 7 |
| 48 | +``` |
| 49 | + |
| 50 | +## Example 2 |
| 51 | + |
| 52 | +In this example, the string str contains the text `"こんにちは、世界!"`. We specify the starting index as `0` and the code point offset as `5`. The `**offsetByCodePoints()**` method returns the new index after applying the offset, which is `5` in this case. It means that the character at index `5` is the desired character. |
| 53 | + |
| 54 | +```java |
| 55 | +class OffsetByCodePoints { |
| 56 | + public static void main(String[] args) { |
| 57 | + String str = "こんにちは、世界!"; |
| 58 | + int startIndex = 0; |
| 59 | + int offsetIndex = 5; |
| 60 | + |
| 61 | + int newIndex = str.offsetByCodePoints(startIndex, offsetIndex); |
| 62 | + System.out.println("New Index: " + newIndex); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +The code above results in the following output: |
| 68 | + |
| 69 | +``` |
| 70 | +New Index: 5 |
| 71 | +``` |
0 commit comments