In Java, Character.codePointAt(seq, index) returns the code point that starts at index. If the char there is a high surrogate and the next char is a low surrogate, both are combined into one supplementary code point; otherwise the char value itself is returned.
It is the static, general version of String.codePointAt(): it accepts any CharSequence (a String, StringBuilder, and so on) and also plain char[] arrays, with an optional limit for arrays.
A static method of java.lang.Character (Java 5+): call it on the class, as in Character.codePointAt(...); you do not need a Character object. No import is needed, because java.lang is always available.
Syntax
Three overloads: a char sequence, a char array, and a char array with a limit.
Java
String s = "A\uD83D\uDE00B"; // A, emoji U+1F600, B
int a = Character.codePointAt(s, 0); // 65
int b = Character.codePointAt(s, 1); // 128512 (the whole emoji)
int c = Character.codePointAt(s, 2); // 56832 (a lone low surrogate)
int d = Character.codePointAt(s.toCharArray(), 1, 2); // 55357, limit stops before the low half
Call
What it does
Character.codePointAt(seq, i)
Code point at index i of any CharSequence.
Character.codePointAt(chars, i)
The same for a char[].
Character.codePointAt(chars, i, limit)
Only looks at indexes below limit.
The index counts char positions, not characters. Starting on the second half of a pair returns that low surrogate on its own.
Library declarations
Java
// java.lang.Character (since Java 5)
public static int codePointAt(CharSequence seq, int index)
public static int codePointAt(char[] a, int index)
public static int codePointAt(char[] a, int index, int limit)
Parameters
Parameter
Description
seq / a
The text to read. Must not be null.
index (int)
The char index to start at, from 0 to length - 1 (or limit - 1).
limit (int)
Array overload only: the first index that may not be read; at most a.length.
Return Value
Return Value
Description
int
The supplementary code point if a valid pair starts at index, otherwise the char value at index.
Combines two chars you already have, without checking them.
Java Examples
Example 1 — What codePointAt Returns at Each Index
Read all four char positions of a string that holds an emoji between two letters.
Java
public class CharacterCodePointAtIndexes {
public static void main(String[] args) {
String s = "A\uD83D\uDE00B";
for (int i = 0; i < s.length(); i++) {
System.out.println("index " + i + " -> " + Character.codePointAt(s, i));
}
}
}
Output
index 0 -> 65
index 1 -> 128512
index 2 -> 56832
index 3 -> 66
How It Works
A plain letter. Index 0 holds 'A', which is returned as 65.
A pair joined. Index 1 holds the high surrogate 0xD83D and index 2 the low surrogate 0xDE00, so the call at 1 returns the full emoji 128512 (0x1F600).
Starting in the middle. At index 2 there is no high surrogate in front from the method's point of view, so the low surrogate comes back alone as 56832; this is why loops step with charCount.
Example 2 — Arrays, a Limit, and an Index Past the Limit
Use the array overloads, then let a limit cut a surrogate pair in half.
Java
public class CharacterCodePointAtLimit {
public static void main(String[] args) {
char[] a = {'x', '\uD83D', '\uDE00'};
System.out.println("no limit: " + Character.codePointAt(a, 1));
System.out.println("limit 2: " + Character.codePointAt(a, 1, 2));
try {
Character.codePointAt(a, 2, 2);
} catch (IndexOutOfBoundsException e) {
System.out.println("index 2 with limit 2: IndexOutOfBoundsException");
}
}
}
Output
no limit: 128512
limit 2: 55357
index 2 with limit 2: IndexOutOfBoundsException
How It Works
Arrays work like strings. Without a limit, the pair at indexes 1 and 2 is read as one code point, 128512.
The limit hides the low half. With limit 2, index 2 is off limits, so only the high surrogate is seen and returned as 55357.
The index must be below the limit. Asking for index 2 with limit 2 throws IndexOutOfBoundsException, which the catch reports.
Example 3 — Read Code Points from a StringBuilder
codePointAt accepts any CharSequence. Walk a StringBuilder by code point, then read past the end.
Java
public class CharacterCodePointAtBuilder {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("ok");
sb.appendCodePoint(0x1D11E);
for (int i = 0; i < sb.length(); ) {
int cp = Character.codePointAt(sb, i);
System.out.println(i + ": 0x" + Integer.toHexString(cp));
i += Character.charCount(cp);
}
try {
Character.codePointAt(sb, sb.length());
} catch (IndexOutOfBoundsException e) {
System.out.println("index " + sb.length() + ": out of bounds");
}
}
}
Output
0: 0x6f
1: 0x6b
2: 0x1d11e
index 4: out of bounds
How It Works
Build without escapes.appendCodePoint(0x1D11E) adds MUSICAL SYMBOL G CLEF as two chars, so the builder has length 4.
Step by code point. The loop visits indexes 0, 1, and 2 and then jumps by 2, so the G clef is read once as 0x1d11e.
One past the end. Index 4 equals the length, which is not a valid position; the call throws IndexOutOfBoundsException.
Frequently Asked Questions
String.codePointAt is an instance method for Strings only. Character.codePointAt is static and works with any CharSequence and with char arrays.
The low surrogate is returned by itself, because the method only looks forward from the index. Step past whole code points with Character.charCount to avoid this.
IndexOutOfBoundsException, or its subclass StringIndexOutOfBoundsException when the sequence is a String. A null argument throws NullPointerException.
With a char array, limit marks where valid data ends. The method will not combine a high surrogate with a low surrogate at or beyond the limit.