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')
Call
What 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
Parameter
Description
seq / a
The 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 Value
Description
int
The supplementary code point if a valid pair ends at index - 1, otherwise the char value at index - 1.
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));
}
}
}
Output
before 1 -> 65
before 2 -> 55357
before 3 -> 128512
before 4 -> 66
How It Works
The loop starts at 1. There is nothing before index 0, so the valid range is 1 to s.length() inclusive.
Half a pair. Before index 2 sits only the high surrogate 0xD83D (55357); the low half is not part of the look-back.
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());
}
}
Output
from the end: 121 128512 120
How It Works
Begin past the last char.i = s.length() is 4, and codePointBefore(s, 4) returns 'y' (121).
Jump over the pair. At i = 3 the method returns the emoji, and charCount moves i back by 2, straight to 1.
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.