In Java, codePointBefore() returns the Unicode code point that ends just before a given index. Use it when you walk a string from right to left.
You pass one index. It is required and must be greater than 0 and at most length(). The result is an int.
Syntax
Call codePointBefore on the string. Start with this line.
java
int cp = text.codePointBefore(text.length());
Call
What it does
text.codePointBefore(text.length())
Returns the last Unicode code point in the string.
Index 0 is never valid. Step back with Character.charCount when you walk left through surrogate pairs.
Library declarations
java
public int codePointBefore(int index);
Parameters
Parameter
Description
index
Required. The index after the code point you want. Must be from 1 to length().
Return Value
Return Value
Description
int
The Unicode code point before that index.
Java Examples
Example 1 — Character Left of a Cursor
For BMP text, codePointBefore(1) returns the first letterβ the unit sitting immediately left of index 1.
java
public class CodePointBeforeBasic {
public static void main(String[] args) {
String word = "Java";
int cp = word.codePointBefore(1);
System.out.println("codePointBefore(1) = " + cp);
System.out.println("as char = " + (char) cp);
}
}
Output
codePointBefore(1) = 74
as char = J
How It Works
Start with text."Java" has length 4; units sit at indexes 0β3.
Place the cursor at 1. That gap is between 'J' and 'a'.
Look left.codePointBefore(1) returns the code point for 'J' (74).
Cast for display. For BMP letters, (char) cp prints the glyph.
Example 2 — Last Character Including Emoji
codePointBefore(length()) returns the last code pointβ even when it is a two-unit emoji. Compare with codePointAt at the emoji’s start index.
java
public class CodePointBeforeEmoji {
public static void main(String[] args) {
String text = "Hiπ";
int last = text.codePointBefore(text.length());
int at = text.codePointAt(2);
System.out.println("length() = " + text.length());
System.out.println("before(length) = 0x" + Integer.toHexString(last).toUpperCase());
System.out.println("at(2) = 0x" + Integer.toHexString(at).toUpperCase());
}
}
Map the string.'H', 'i', then a surrogate pair for π (indexes 2β3).
Cursor at the end.length() is 4βthe gap after the low surrogate.
Look left.codePointBefore(4) combines both surrogates β U+1F600.
Same emoji forward.codePointAt(2) starts at the high surrogate and returns the same value.
Example 3 — Walk the String Backward
Start the cursor at length(), read with codePointBefore, then step back by Character.charCount(cp). For larger jumps, see offsetByCodePoints().
java
public class CodePointBeforeReverse {
public static void main(String[] args) {
String text = "AπB";
for (int i = text.length(); i > 0; ) {
int cp = text.codePointBefore(i);
System.out.println("before " + i + " β 0x" + Integer.toHexString(cp).toUpperCase());
i -= Character.charCount(cp);
}
}
}
Output
before 4 β 0x42
before 3 β 0x1F600
before 1 β 0x41
How It Works
Start at the end. Cursor i = length() (4 for "AπB").
Read left.codePointBefore(4) β 'B'; then subtract 1 β cursor 3.
Handle the emoji.codePointBefore(3) β full π; charCount is 2 β cursor 1.
Finish at the start.codePointBefore(1) β 'A'; subtract 1 β loop ends.
Frequently Asked Questions
codePointBefore() returns the Unicode code point that ends just before a given index.
index is required. Valid values are from 1 to length().
An int Unicode code point.
No. Index 0 throws StringIndexOutOfBoundsException.
Use it when scanning a string from right to left by code points.