Java Character codePointBefore() Method

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

Definition

In Java, Character.codePointBefore(seq, index) looks at the char at index - 1. If it is a low surrogate and the char before it is a high surrogate, the two are combined and the supplementary code point is returned; otherwise the single char value at index - 1 is returned.

It is the mirror image of codePointAt() and is mainly used to walk text backwards, one whole character at a time. The array overload can take a start index that it will not read before.

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

Syntax

index is the position just after the code point you want.

Java
String s = "A\uD83D\uDE00B";
int a = Character.codePointBefore(s, 1);   // 65 ('A')
int b = Character.codePointBefore(s, 2);   // 55357, only the high surrogate is before index 2
int c = Character.codePointBefore(s, 3);   // 128512, the whole emoji
int d = Character.codePointBefore(s, 4);   // 66 ('B')
CallWhat it does
Character.codePointBefore(seq, i)Code point that ends at i - 1.
Character.codePointBefore(seq, seq.length())The last code point of the text.
Character.codePointBefore(chars, i, start)Never look at indexes below start.

Valid indexes run from 1 to the length (inclusive), because there must be at least one char before index.

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

Parameters

ParameterDescription
seq / aThe text to read. Must not be null.
index (int)The index just after the code point, from 1 to the length.
start (int)Array overload only: the lowest index that may be read.

Return Value

Return ValueDescription
intThe supplementary code point if a valid pair ends at index - 1, otherwise the char value at index - 1.

Compare with Similar Methods

Reading code points in both directions:

MethodHow it differs
codePointAt(seq, i)Reads forward from i.
String.codePointBefore(i)The instance method on String.
offsetByCodePoints(seq, i, -1)Finds the index of the previous code point.

Java Examples

Example 1 — What codePointBefore Returns at Each Index

Check positions 1 to 4 of the four-char string "A, emoji, B".

Java
public class CharacterCodePointBeforeIndexes {
    public static void main(String[] args) {
        String s = "A\uD83D\uDE00B";
        for (int i = 1; i <= s.length(); i++) {
            System.out.println("before " + i + " -> " + Character.codePointBefore(s, i));
        }
    }
}

How It Works

  1. The loop starts at 1. There is nothing before index 0, so the valid range is 1 to s.length() inclusive.
  2. Half a pair. Before index 2 sits only the high surrogate 0xD83D (55357); the low half is not part of the look-back.
  3. The whole emoji. Before index 3 the method sees the low surrogate, checks that a high surrogate precedes it, and returns 128512.

Example 2 — Iterate a String from the End by Code Point

Start at the length and step back with charCount, so the emoji is read as one character.

Java
public class CharacterCodePointBeforeReverse {
    public static void main(String[] args) {
        String s = "x\uD83D\uDE00y";
        StringBuilder order = new StringBuilder();
        int i = s.length();
        while (i > 0) {
            int cp = Character.codePointBefore(s, i);
            order.append(cp).append(' ');
            i -= Character.charCount(cp);
        }
        System.out.println("from the end: " + order.toString().trim());
    }
}

How It Works

  1. Begin past the last char. i = s.length() is 4, and codePointBefore(s, 4) returns 'y' (121).
  2. Jump over the pair. At i = 3 the method returns the emoji, and charCount moves i back by 2, straight to 1.
  3. Three code points, not four chars. The last step reads 'x' (120) and the loop ends at 0.

Example 3 — The start Argument Can Split a Pair

Read the end of an emoji with and without a start boundary, then use an invalid index.

Java
public class CharacterCodePointBeforeStart {
    public static void main(String[] args) {
        char[] a = {'\uD83D', '\uDE00', 'z'};
        System.out.println("before 2:          " + Character.codePointBefore(a, 2));
        System.out.println("before 2, start 1: " + Character.codePointBefore(a, 2, 1));
        try {
            Character.codePointBefore(a, 0);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("before 0: IndexOutOfBoundsException");
        }
    }
}

How It Works

  1. Full pair visible. Indexes 0 and 1 hold the high and low halves, so the look-back from index 2 returns 128512.
  2. start hides index 0. With start 1, the high surrogate is outside the allowed range, so only the low surrogate (56832) is returned.
  3. Nothing before 0. Index 0 has no char in front of it, so the call throws IndexOutOfBoundsException.

Frequently Asked Questions

It returns the code point that ends just before the given index, combining a surrogate pair when the last two chars before the index form one.
From 1 to the length of the sequence, inclusive. Index 0 throws IndexOutOfBoundsException because nothing comes before it.
Start with i = s.length(), read cp = Character.codePointBefore(s, i), then do i -= Character.charCount(cp) until i reaches 0.
For char arrays only: it is the lowest index the method may read, so a high surrogate before start is not combined.
The low surrogate value itself, such as 56832 for 0xDE00. The method does not throw for unpaired surrogates.

Next: codePointCount()

Count the code points (real characters) in a range of text.

codePointCount() 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.

10 people found this page helpful