Home Java StringBuilder Methods codePointBefore() Java StringBuilder codePointBefore() Method Definition In Java, sb.codePointBefore(index) returns the int Unicode code point that ends immediately before UTF-16 cursor position index inside a StringBuilder.
If the character right before index is the low surrogate of a valid surrogate pair, it combines both halves into the full supplementary code point. Paired with delete() , it lets you pop the last whole Unicode character (even an emoji) cleanly.
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.codePointBefore(...). No import is needed because java.lang is always visible.
Syntax Pass a cursor position from 1 through sb.length() inclusive.
StringBuilder sb = new StringBuilder("A\\uD83D\\uDE00B");
int emoji = sb.codePointBefore(3); // 128512 (0x1F600)
int last = sb.codePointBefore(sb.length()); // 66 ('B') Call What it does sb.codePointBefore(sb.length())Reads the final Unicode code point in the builder. sb.codePointBefore(cursor)Reads the code point immediately left of cursor. cursor -= Character.charCount(cp)Steps a backward loop past 1 or 2 char units.
Valid indexes for codePointBefore() are 1 to length() inclusive—never 0.
Library declarations // java.lang.StringBuilder
public int codePointBefore(int index) Parameters Parameter Description index (int)UTF-16 cursor index right after the target code point (1 <= index <= length()).
Return Value Return Value Description intThe Unicode code point immediately before index.
Important
Passing 0, a negative number, or anything greater than length() throws IndexOutOfBoundsException. Empty builders have no valid index for codePointBefore(). Never use deleteCharAt(sb.length() - 1) to drop a trailing emoji—inspect codePointBefore(sb.length()) first so you delete both surrogate halves together.
Compare with Similar Methods Inspecting and deleting code points backward:
Java Examples 1. Safe backspace delete 2. Cursor indexes on surrogate pair 3. Backward loop and index 0 Example 1 — Pop the Last Unicode Character Safely (Backspace Pattern) Use codePointBefore(sb.length()) to delete the entire trailing emoji pair rather than leaving a broken half-surrogate.
public class StringBuilderCodePointBeforePop {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("A\uD83D\uDE00");
System.out.println("Start length = " + sb.length());
int cp = sb.codePointBefore(sb.length());
int width = Character.charCount(cp);
sb.delete(sb.length() - width, sb.length());
System.out.println("Popped 0x" + Integer.toHexString(cp).toUpperCase() + " (width " + width + ")");
System.out.println("Remaining: " + sb.toString() + " (length=" + sb.length() + ")");
}
} Start length = 3
Popped 0x1F600 (width 2)
Remaining: A (length=1) How It Works Read trailing symbol. codePointBefore(3) merges indexes 1 and 2 into 0x1F600.Measure UTF-16 span. Character.charCount(cp) returns 2, telling us how far back to delete.Remove full character. Deleting indexes 1..3 leaves only "A" with zero leftover surrogate halves.Example 2 — Inspect Positions Before Each Code Point Check cursor positions 1, 3, and 4 on "A\\uD83D\\uDE00B".
public class StringBuilderCodePointBeforePair {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("A\uD83D\uDE00B");
System.out.println("before(1) = 0x" + Integer.toHexString(sb.codePointBefore(1)).toUpperCase());
System.out.println("before(3) = " + sb.codePointBefore(3) + " (0x" + Integer.toHexString(sb.codePointBefore(3)).toUpperCase() + ")");
System.out.println("before(4) = 0x" + Integer.toHexString(sb.codePointBefore(4)).toUpperCase());
}
} before(1) = 0x41
before(3) = 128512 (0x1F600)
before(4) = 0x42 How It Works Cursor after 'A'. Position 1 sits between 'A' and the emoji, returning 0x41.Cursor after emoji. Position 3 looks left across indexes 2 and 1, uniting the surrogate pair into 128512.Cursor at length(). Position 4 sits after 'B' and returns 0x42.Example 3 — Walk a Builder Backward and Catch Index 0 Scan right to left across a builder using Character.charCount(), then confirm codePointBefore(0) throws.
public class StringBuilderCodePointBeforeLoop {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("X\uD83D\uDE80Y");
for (int pos = sb.length(); pos > 0; ) {
int cp = sb.codePointBefore(pos);
System.out.println("before " + pos + " -> 0x" + Integer.toHexString(cp).toUpperCase());
pos -= Character.charCount(cp);
}
try {
sb.codePointBefore(0);
} catch (IndexOutOfBoundsException e) {
System.out.println("Index 0 threw IndexOutOfBoundsException");
}
}
} before 4 -> 0x59
before 3 -> 0x1F680
before 1 -> 0x58
Index 0 threw IndexOutOfBoundsException How It Works Step left cleanly. Subtracting Character.charCount(cp) jumps from cursor 3 straight to 1 over the two-char emoji.Three characters visited. The loop reads 'Y', U+1F680, and 'X' in reverse order.Zero throws. Nothing lies before index 0, so codePointBefore(0) raises IndexOutOfBoundsException.Frequently Asked Questions Why does sb.codePointBefore(0) throw an exception? Because no character exists before index 0. Valid cursor positions run from 1 to sb.length() inclusive.
How do I remove the last full Unicode character from a StringBuilder? Read cp = sb.codePointBefore(sb.length()), then call sb.delete(sb.length() - Character.charCount(cp), sb.length()).
What does sb.codePointBefore(3) return on "A\uD83D\uDE00B"? It returns 128512 (0x1F600), because indexes 1 and 2 hold the high and low surrogate pair ending right before index 3.
What exception does codePointBefore() throw for invalid indexes? IndexOutOfBoundsException when index < 1 or index > sb.length().
Does codePointBefore() modify the StringBuilder? No. It only reads the code point and leaves the builder unchanged.
Next: codePointCount() Count the Unicode code points in a range, treating each surrogate pair as one character.
codePointCount() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion