Java Character offsetByCodePoints() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Character

Definition

In Java, Character.offsetByCodePoints(seq, index, codePointOffset) starts at char index index and moves by codePointOffset code points, returning the new char index. A positive offset moves forward, a negative offset moves backward, and a surrogate pair always counts as one step.

It answers questions such as "where does the third character end?" so that you can call substring without cutting an emoji in half. The char array overload limits the search to a sub-array given by start and count.

A static method of java.lang.Character (Java 5+): call it on the class, as in Character.offsetByCodePoints(...); you do not need a Character object. No import is needed, because java.lang is always available.

Syntax

Pass the text, a start index, and how many code points to move.

Java
String s = "A\uD83D\uDE00B";                          // 4 chars, 3 code points
int i1 = Character.offsetByCodePoints(s, 0, 1);         // 1
int i2 = Character.offsetByCodePoints(s, 0, 2);         // 3 (jumped over the pair)
int i3 = Character.offsetByCodePoints(s, 4, -2);        // 1 (moving backward)
CallWhat it does
s.substring(0, Character.offsetByCodePoints(s, 0, n))The first n characters, never splitting a pair.
Character.offsetByCodePoints(s, i, 1)Index of the next code point.
Character.offsetByCodePoints(s, i, -1)Index of the previous code point.

Array form: offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset), where start and count describe the part of the array that may be used and the result is an index into a.

Library declarations
Java
// java.lang.Character (since Java 5)
public static int offsetByCodePoints(CharSequence seq, int index, int codePointOffset)
public static int offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset)

Parameters

ParameterDescription
seq / aThe text. Must not be null.
index (int)The char index to start from, 0 to the length.
codePointOffset (int)How many code points to move; negative moves backward.
start, countArray overload only: the usable sub-array.

Return Value

Return ValueDescription
intThe char index reached after moving codePointOffset code points.

Compare with Similar Methods

Related helpers:

MethodHow it differs
codePointCount(seq, b, e)The opposite question: how many code points lie between two indexes.
charCount(cp)Steps over one code point you have already read.
String.offsetByCodePoints(i, n)The same operation as an instance method on String.

Java Examples

Example 1 — Forward and Backward Through an Emoji

Move from the start and from the end of the string "A, emoji, B".

Java
public class CharacterOffsetByCodePointsMoves {
    public static void main(String[] args) {
        String s = "A\uD83D\uDE00B";
        for (int n = 1; n <= 3; n++) {
            System.out.println("from 0 by " + n + " -> " + Character.offsetByCodePoints(s, 0, n));
        }
        System.out.println("from 4 by -1 -> " + Character.offsetByCodePoints(s, 4, -1));
        System.out.println("from 4 by -2 -> " + Character.offsetByCodePoints(s, 4, -2));
    }
}

How It Works

  1. One code point, one or two chars. Moving past 'A' adds 1 to the index; moving past the emoji adds 2, so the second step lands on 3.
  2. The end is a valid result. Three steps from 0 reach index 4, which equals the length.
  3. Backward moves. From 4, one step back skips 'B' (to 3) and two steps also skip the emoji pair (to 1).

Example 2 — Keep the First Three Characters Without Breaking an Emoji

Compare a naive substring(0, 3) with one that uses offsetByCodePoints.

Java
public class CharacterOffsetByCodePointsTruncate {
    public static void main(String[] args) {
        String s = "ab\uD83D\uDE00cd";
        String naive = s.substring(0, 3);
        int end = Character.offsetByCodePoints(s, 0, 3);
        String safe = s.substring(0, end);
        System.out.println("naive: length " + naive.length() + ", ends with high surrogate? "
                + Character.isHighSurrogate(naive.charAt(naive.length() - 1)));
        System.out.println("safe:  length " + safe.length() + ", code points "
                + safe.codePointCount(0, safe.length()));
    }
}

How It Works

  1. A broken cut. substring(0, 3) keeps a, b, and only the first half of the emoji, leaving a dangling high surrogate.
  2. Find the real end. Three code points from index 0 end at index 4, because the emoji occupies indexes 2 and 3.
  3. A clean result. The safe version has 4 chars that form exactly 3 complete characters: a, b, and the emoji.

Example 3 — Asking for Too Many Code Points, and the Array Overload

Move further than the text allows in each direction, then use the five-argument array version.

Java
public class CharacterOffsetByCodePointsErrors {
    public static void main(String[] args) {
        String s = "A\uD83D\uDE00B";
        try {
            Character.offsetByCodePoints(s, 0, 5);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("0 by 5: IndexOutOfBoundsException");
        }
        try {
            Character.offsetByCodePoints(s, 0, -1);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("0 by -1: IndexOutOfBoundsException");
        }
        char[] a = {'x', '\uD83D', '\uDE00', 'y'};
        System.out.println("array, start 0, count 4, from 0 by 2 -> "
                + Character.offsetByCodePoints(a, 0, 4, 0, 2));
    }
}

How It Works

  1. Not enough code points ahead. The string has only 3 code points, so moving 5 forward from index 0 throws.
  2. Nothing behind. A negative offset from index 0 asks for a code point before the start, which also throws IndexOutOfBoundsException.
  3. The array form. Within the sub-array start 0, count 4, two steps from index 0 pass 'x' and the emoji pair and stop at index 3.

Frequently Asked Questions

A char index: the position reached after moving the given number of code points from the start index.
Yes. A negative offset moves backward. It throws IndexOutOfBoundsException if there are not enough code points before the index.
s.substring(0, Character.offsetByCodePoints(s, 0, n)). This never splits a surrogate pair, but it throws if s has fewer than n code points.
They describe the sub-array the method may use: indexes start to start + count - 1. The result is still an index into the whole array.
Yes. s.offsetByCodePoints(index, codePointOffset) does the same thing for a String.

Next: reverseBytes()

Swap the two bytes of a char, converting between big-endian and little-endian.

reverseBytes() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful