Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
Title: '.offsetByCodePoints()'
Description: 'Returns the new index of a character in a string after applying the specified code point offset.'
Subjects:
- 'Computer Science'
Tags:
- 'Strings'
- 'Methods'
CatalogContent:
- 'learn-java'
- 'paths/computer-science'
---

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.

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.

## Syntax

```pseudo
Strings.offsetByCodePoints(startIndex, offsetIndex)
```

- `startIndex` (int): The starting index in the string from which the offset is applied.
- `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.

## Example 1

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.

```java
class OffsetByCodePoints {
public static void main(String[] args) {
String str = "Hello, World!";
int startIndex = 0;
int offsetIndex = 7;

int newIndex = str.offsetByCodePoints(startIndex, offsetIndex);
System.out.println("New Index: " + newIndex);
}
}
```

The code above results in the following output:

```
New Index: 7
```

## Example 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You haven't addressed my issue with the duplicative examples:

The next two examples are essentially the same as the first one. It might be good to show an example of fetching a character from the middle of a string of completely non-Latin script.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SSwiniarski In Example 3, I have added a completely non-latin script.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove example two, as it pretty much duplicates example one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure


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.

```java
class OffsetByCodePoints {
public static void main(String[] args) {
String str = "こんにちは、世界!";
int startIndex = 0;
int offsetIndex = 5;

int newIndex = str.offsetByCodePoints(startIndex, offsetIndex);
System.out.println("New Index: " + newIndex);
}
}
```

The code above results in the following output:

```
New Index: 5
```