In Java, offsetByCodePoints(index, codePointOffset) returns the char index you reach when you start at index and move codePointOffset whole Unicode characters forward (positive) or backward (negative).
A surrogate pair, such as an emoji, counts as one step even though it occupies two chars. That makes the method the safe way to find "the n-th character" in text that may contain emoji, before calling codePointAt or cutting with substring.
offsetByCodePoints() is an instance method of java.lang.String, added in Java 5 (JDK 1.5). Call it on a string, as in text.offsetByCodePoints(...); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed.
Syntax
Pass a starting char index and how many code points to move.
java
int start = text.offsetByCodePoints(0, n); // index of the n-th code point
int back = text.offsetByCodePoints(text.length(), -1); // start of the last code point
Call
What it does
"Hello".offsetByCodePoints(0, 3)
3: for plain text it equals simple addition.
s.offsetByCodePoints(0, 1)
Index after the first code point (1 or 2).
s.offsetByCodePoints(i, -1)
Start of the code point before index i.
The result is always a char index, so you can pass it directly to charAt, codePointAt, or substring. Inside the range, an unpaired surrogate counts as one code point.
Library declarations
java
public int offsetByCodePoints(int index, int codePointOffset);
Parameters
Parameter
Description
index
The char index to start from (0 to length()).
codePointOffset
How many code points to move; negative moves backward.
Return Value
Return Value
Description
int
The char index reached after moving.
Java Examples
Example 1 — Plain Text: Same as Adding to the Index
Without emoji or other supplementary characters, one code point is one char.
java
public class OffsetByCodePointsAscii {
public static void main(String[] args) {
String word = "Hello";
System.out.println(word.offsetByCodePoints(0, 3));
System.out.println(word.offsetByCodePoints(1, 2));
System.out.println(word.charAt(word.offsetByCodePoints(0, 4)));
}
}
Output
3
3
o
How It Works
No surrogates, no surprises. Every character of "Hello" is a single char, so moving 3 code points from 0 lands on index 3.
Any starting point. From index 1, two steps also reach 3, just like 1 + 2.
Feed it to charAt. Four steps from the start reach index 4, the final 'o'.
Example 2 — Find Characters After Emoji
A string with two smileys: each one is a single step but two chars wide.
java
public class OffsetByCodePointsEmoji {
public static void main(String[] args) {
String s = "a\uD83D\uDE00b\uD83D\uDE00c";
int afterTwo = s.offsetByCodePoints(0, 2);
int afterFour = s.offsetByCodePoints(0, 4);
int afterOne = s.offsetByCodePoints(0, 1);
System.out.println("length: " + s.length());
System.out.println("skip 2 -> index " + afterTwo + " (" + s.charAt(afterTwo) + ")");
System.out.println("skip 4 -> index " + afterFour + " (" + s.charAt(afterFour) + ")");
System.out.println("skip 1 -> 0x" + Integer.toHexString(s.codePointAt(afterOne)));
}
}
Output
length: 7
skip 2 -> index 3 (b)
skip 4 -> index 6 (c)
skip 1 -> 0x1f600
How It Works
Seven chars, five code points. The text is a, smiley, b, smiley, c; each smiley (U+1F600) takes two chars, so length() is 7.
Whole emoji per step. Skipping a and the first smiley lands on index 3 ('b'); skipping four code points lands on index 6 ('c'), not on a half emoji.
Read the emoji safely. One step from 0 is index 1, where codePointAt reads the complete smiley, printed in hex as 1f600.
Example 3 — Move Backwards and Hit the Limits
Negative offsets step left; asking for more code points than exist throws.
java
public class OffsetByCodePointsBack {
public static void main(String[] args) {
String s = "a\uD83D\uDE00b\uD83D\uDE00c";
System.out.println(s.offsetByCodePoints(s.length(), -1));
System.out.println(s.offsetByCodePoints(6, -1));
System.out.println(s.offsetByCodePoints(6, -3));
try {
s.offsetByCodePoints(0, 10);
} catch (IndexOutOfBoundsException e) {
System.out.println("only 5 code points: IndexOutOfBoundsException caught");
}
}
}
Output
6
4
1
only 5 code points: IndexOutOfBoundsException caught
How It Works
Last code point. From the end (index 7) one step back reaches 6, where the single-char 'c' begins.
Back over a pair. From 6, one step left jumps over both halves of the second smiley to its start at index 4; three steps (smiley, b, smiley) reach index 1.
Not enough characters. The string holds only 5 code points, so a move of 10 throws IndexOutOfBoundsException.
Frequently Asked Questions
It returns the char index you reach after moving a given number of Unicode code points from a starting index, forward or backward.
Because characters such as emoji take two chars. Adding 1 could land in the middle of a surrogate pair; offsetByCodePoints always moves by whole characters.
Yes. Pass a negative codePointOffset, for example s.offsetByCodePoints(s.length(), -1) for the start of the last code point.
IndexOutOfBoundsException when the start index is outside 0..length() or there are not enough code points to complete the move.
Compute int i = s.offsetByCodePoints(0, n) and then read s.codePointAt(i).