STRING INSTANCE METHODS
Java String offsetByCodePoints() method
offsetByCodePoints() returns a position in a String string shifted from a starting index by a specified number of Unicode code points. It is useful for emoji and other characters using two UTF-16 char units.
Moving an index by code points
String tekstas = "Hello World";
int indeksas = tekstas.offsetByCodePoints(2, 6);
System.out.println(indeksas);8
tekstasstores Hello World as aString.2is the starting UTF-16 index, pointing to the first letter,l.6means an offset of six Unicode code points forward.- Each code point in this text occupies one
char, so the returned index is2 + 6 = 8.
Why is the method called on a String object?
String.offsetByCodePoints() is an instance method. Call it on a specific string and supply only the starting index and offset:
int indeksas = tekstas.offsetByCodePoints(2, 6);
Here, tekstas is String object. String.offsetByCodePoints(tekstas, 2, 6) would be invalid because this variant is not static and has no string parameter.
Method syntax
public int offsetByCodePoints(int index, int codePointOffset)
| Part | Meaning |
|---|---|
| Calling object | String string in which to find the new index. |
index | Starting UTF-16 char index from 0 to length(), inclusive at both ends. |
codePointOffset | Code-point count: positive moves forward, negative moves backwards, 0 keeps the same index. |
| Return type | int – a new UTF-16 char index, not the code point itself. |
| Error | IndexOutOfBoundsExceptionif the index or offset exceeds the string's bounds. |
Another forward-offset example
String tekstas = "geeks for geeks";
int indeksas = tekstas.offsetByCodePoints(3, 8);
System.out.println(indeksas);11
Starting at index 3 and moving eight code points gives index 11. Spaces are code points too and count towards the offset.
Code points and UTF-16 indices differ
String tekstas = "A😀B";
int indeksas = tekstas.offsetByCodePoints(0, 2);
System.out.println(tekstas.length());
System.out.println(indeksas);
System.out.println(tekstas.charAt(indeksas));4
3
B
A😀B has three Unicode code points, but length() returns 4because 😀 is a surrogate pair of two UTF-16 units. Moving from 0 by two code points, A and 😀 , reaches B at index 3, not 2.
Unpaired surrogate char units each count as one code point in this method.
A negative offset moves backwards
String tekstas = "A😀B";
System.out.println(tekstas.offsetByCodePoints(3, -1));
System.out.println(tekstas.offsetByCodePoints(3, -2));1
0
The index 3 is before B. The offset -1 skips the whole emoji pair and returns its starting index, 1. The offset -2 reaches A at its starting index, 0.
Zero and the position after the last character
String tekstas = "Java";
System.out.println(tekstas.offsetByCodePoints(2, 0));
System.out.println(tekstas.offsetByCodePoints(0, 4));
System.out.println(tekstas.offsetByCodePoints(4, 0));2
4
4
The offset 0 returns the starting index. The index tekstas.length()is also valid: it marks the position just after the last char, so tekstas.offsetByCodePoints(0, 4) and tekstas.offsetByCodePoints(4, 0) both return 4.
The difference from Character.offsetByCodePoints()
String tekstas = "A😀B";
System.out.println(tekstas.offsetByCodePoints(0, 2));
System.out.println(Character.offsetByCodePoints(tekstas, 0, 2));3
3
tekstas.offsetByCodePoints(0, 2) is String instance method: the string is the calling object. Character.offsetByCodePoints(tekstas, 0, 2) is a separate static method receiving the text as a CharSequence argument. Both return the same UTF-16 index in this example.
The starting index must be within the string
String tekstas = "Java";
System.out.println(tekstas.offsetByCodePoints(5, 0));IndexOutOfBoundsException
Java has length 4, so valid starting indices range from 0 to 4. Index 5 is invalid even when the offset is 0.
A positive offset that is too large
String tekstas = "Java";
System.out.println(tekstas.offsetByCodePoints(3, 2));IndexOutOfBoundsException
From index 3 to Java to the end, only one code point remains: a. The offset 2 requires more than that.
A negative offset that is too large
String tekstas = "Java";
System.out.println(tekstas.offsetByCodePoints(1, -2));IndexOutOfBoundsException
Before index 1 there is only one code point, J. The offset -2 would move one code point before the start of the string.
The empty string
String tekstas = "";
System.out.println(tekstas.offsetByCodePoints(0, 0));
System.out.println(tekstas.offsetByCodePoints(0, 1));0
IndexOutOfBoundsException
An empty string's only valid index is 0. The offset 0 returns that index, but a positive offset has no code points to traverse, so the second call throws an error.
Handling an error
String tekstas = "Java";
int pradinisIndeksas = 3;
int poslinkis = 2;
try {
System.out.println(tekstas.offsetByCodePoints(pradinisIndeksas, poslinkis));
} catch (IndexOutOfBoundsException e) {
System.out.println("Nepakanka kodo taškų");
}Nepakanka kodo taškų
If an offset comes from user input, first check the available code points in that direction or handle the IndexOutOfBoundsException. The starting index must still range from 0 to tekstas.length().
Common mistakes
- Treating the return value as a code point, though it is a UTF-16
charindex. - Simply adding
codePointOffsetto the index, even though emoji may occupy twocharunits. Stringinstance method is confused with the staticCharactervariant, which also accepts aCharSequenceargument to the method.- Ignoring negative offsets or the number of code points remaining before the string boundary.
- The index
length()is treated as invalid although it is the valid position after the lastchar.
When should you use offsetByCodePoints()?
String.offsetByCodePoints() Use it to find a new String index after moving a given number of Unicode code points. It moves forwards or backwards without splitting surrogate pairs. The returned index can be used by other String methods, such as substring() or codePointAt().