Library contents

Java String.codePointAt

Read the Unicode code point starting at a UTF-16 offset.

A simple example

String.codePointAt

  1. Start with "A"
  2. Do this Read the first code point
  3. Result 65
The Unicode code point for the letter A is decimal 65.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println("A".codePointAt(0));
    }
}
Output
65

Syntax

Java syntax
public int codePointAt(int index)
JDK 21 method signatures (1)
public int codePointAt(int index)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Identify an emoji on a shift roster

A feedback or ticket label includes an emoji that needs a full Unicode code point. Inspect the symbol at the right boundary so the value sent to the next component represents the entire character.

From the programString shift = "A😀"
Result128512
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String shift = "A😀";
        System.out.println(shift.codePointAt(1));
    }
}
Output
128512

Index one starts the surrogate pair for U+1F600.

Advertisements

Inspect the first letter on a badge

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 badge = "Émile"
Resultc9
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        String badge = "Émile";
        System.out.println(Integer.toHexString(badge.codePointAt(0)));
    }
}
Output
c9

The precomposed capital E with acute accent has code point U+00C9.

Read a parcel sorting code

The depot scans a parcel marked NC-17. The scanner stores the Unicode code point of the first letter. Which decimal value should appear in the log?

GivenNC-17
Worked result78

What to watch for

Start at a code point boundary. An offset on a low surrogate does not recover the preceding high surrogate.

Documentation for String.codePointAt