Library contents

Java String.codePointBefore

Read the Unicode code point that ends immediately before an offset.

A simple example

String.codePointBefore

  1. Start with "AB"
  2. Do this Read the code point before the end
  3. Result 66
The position immediately before the end contains B.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println("AB".codePointBefore(2));
    }
}
Output
66

Syntax

Java 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.java

Read 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.

From the programString status = "sent😀"
Result128512
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String status = "sent😀";
        System.out.println(status.codePointBefore(status.length()));
    }
}
Output
128512

Starting just beyond the text reads both units of the final emoji.

Advertisements

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.

From the programString seat = "B-14"
ResultB
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String seat = "B-14";
        System.out.println((char) seat.codePointBefore(seat.indexOf('-')));
    }
}
Output
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?

GivenNC-17
Worked result67

What to watch for

The offset must be positive and may equal the string length.

Documentation for String.codePointBefore