Java String.codePointAt
Read the Unicode code point starting at a UTF-16 offset.
A simple example
String.codePointAt
- Start with "A"
- Do this Read the first code point
- Result 65
class Main {
public static void main(String[] args) throws Exception {
System.out.println("A".codePointAt(0));
}
}
65
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.javaIdentify 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.
class Main {
public static void main(String[] args) throws Exception {
String shift = "A😀";
System.out.println(shift.codePointAt(1));
}
}
128512
Index one starts the surrogate pair for U+1F600.
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.
class Main {
public static void main(String[] args) throws Exception {
String badge = "Émile";
System.out.println(Integer.toHexString(badge.codePointAt(0)));
}
}
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?
What to watch for
Start at a code point boundary. An offset on a low surrogate does not recover the preceding high surrogate.