In Java, sb.offsetByCodePoints(index, codePointOffset) starts at UTF-16 position index and steps forward (positive) or backward (negative) by codePointOffset Unicode code points, returning the resulting UTF-16 char index.
Because insert(), delete(), and setLength() take UTF-16 char indexes, offsetByCodePoints() lets you calculate safe cut points that never slice a surrogate pair in half.
An instance method of the java.lang.StringBuilder class (since Java 1.5, JDK 1.5): a final, mutable sequence of UTF-16 characters that is not thread-safe. Call it on an existing builder, as in sb.offsetByCodePoints(...). No import is needed because java.lang is always visible.
Syntax
Pass a starting UTF-16 index and a signed step count in code points.
Java
StringBuilder sb = new StringBuilder("A\\uD83D\\uDE00B");
int afterTwo = sb.offsetByCodePoints(0, 2); // 3 (index of 'B')
int backTwo = sb.offsetByCodePoints(4, -2); // 1 (start of emoji)
Call
What it does
sb.offsetByCodePoints(0, 2)
Finds UTF-16 index after the first 2 code points.
sb.offsetByCodePoints(sb.length(), -1)
Finds start index of the last code point.
sb.offsetByCodePoints(i, 0)
Returns i (validates that i is in 0..length()).
If index lands in the middle of a surrogate pair, moving forward or backward steps past that half so it still counts cleanly.
Library declarations
Java
// java.lang.StringBuilder
public int offsetByCodePoints(int index, int codePointOffset)
Parameters
Parameter
Description
index (int)
Starting UTF-16 index (0 <= index <= length()).
codePointOffset (int)
Number of code points to move: positive for forward, negative for backward, 0 for no move.
Return Value
Return Value
Description
int
The target UTF-16 index after stepping by codePointOffset code points.
Compare with Similar Methods
Navigating and editing StringBuilder by code points:
Example 3 — Truncate to N Code Points Safely and Catch Overrun
Use offsetByCodePoints(0, limit) with setLength() to trim text cleanly, and catch stepping past the end.
Java
public class StringBuilderOffsetTruncate {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("A\uD83D\uDE00BC");
int keepLimit = 2;
if (sb.codePointCount(0, sb.length()) > keepLimit) {
int newEnd = sb.offsetByCodePoints(0, keepLimit);
sb.setLength(newEnd);
}
System.out.println("Truncated length = " + sb.length() + ", codePoints = " + sb.codePointCount(0, sb.length()));
try {
sb.offsetByCodePoints(0, 5);
} catch (IndexOutOfBoundsException e) {
System.out.println("Stepping past end threw IndexOutOfBoundsException");
}
}
}
Output
Truncated length = 3, codePoints = 2
Stepping past end threw IndexOutOfBoundsException
How It Works
Truncate cleanly. Keeping 2 code points stops at UTF-16 length 3, preserving 'A' and the complete emoji.
Check before stepping. Calling codePointCount() first ensures keepLimit exists in the builder.
Overstepping throws. Trying to step forward 5 code points when only 2 remain raises IndexOutOfBoundsException.
Frequently Asked Questions
It returns a zero-based UTF-16 char index inside the StringBuilder after moving codePointOffset code points from index.
Yes. A negative offset moves backward toward index 0, useful for finding the start of the last N characters.
substring(), insert(), and delete() use UTF-16 char indexes. Computing the index with offsetByCodePoints() prevents splitting a surrogate pair in half.
When index is outside 0..length(), or when stepping forward/backward by codePointOffset goes past the end or before index 0.
It returns index unchanged as long as 0 <= index <= length().