-
Notifications
You must be signed in to change notification settings - Fork 4.3k
offsetbycodepoints function added #1590 #2453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9179081
9e435b8
cbc2059
9f20239
70308f7
0712024
460f18a
9b00813
affaf73
509dd7c
e60cc1d
d816967
138a404
f40f08c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You haven't addressed my issue with the duplicative examples:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SSwiniarski In Example 3, I have added a completely non-latin script.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove example two, as it pretty much duplicates example one.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.