Java String.codePointBefore
Read the Unicode code point that ends immediately before an offset.
A simple example
String.codePointBefore
- Start with "AB"
- Do this Read the code point before the end
- Result 66
class Main {
public static void main(String[] args) throws Exception {
System.out.println("AB".codePointBefore(2));
}
}
66
Syntax
public int codePointBefore(int index)
JDK 21 method signatures (1)
public int codePointBefore(int index)Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaRead the final delivery status symbol
A status message ends with a Unicode symbol that can occupy several encoding units. Locate the last complete code point before identifying the status that the user actually selected.
class Main {
public static void main(String[] args) throws Exception {
String status = "sent😀";
System.out.println(status.codePointBefore(status.length()));
}
}
128512
Starting just beyond the text reads both units of the final emoji.
Inspect a seat row before its separator
A compact printed code assigns meaning to particular positions. Locate the correct character for the requested field and check the indexing convention before sending the value to the display.
class Main {
public static void main(String[] args) throws Exception {
String seat = "B-14";
System.out.println((char) seat.codePointBefore(seat.indexOf('-')));
}
}
B
The hyphen starts at index one. The preceding code point is B.
Read a parcel sorting code
The depot scans a parcel marked NC-17. The scanner reads the code point immediately before the dash. What decimal Unicode value does it find?
What to watch for
The offset must be positive and may equal the string length.