Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

String.codePoints — a stream of Unicode code points

String.codePoints() returns an IntStream, combining UTF-16 surrogate pairs instead of processing each char unit separately.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

public IntStream codePoints()
// Usage: text.codePoints()

String.codePoints() returns an IntStream of code point values. It combines valid surrogate pairs into one int value.

Other UTF-16 units, including unpaired surrogates, pass through as nonnegative int values. It therefore does not reject malformed surrogate sequences.

length() counts UTF-16 units, whereas chars() emits them separately. In the example, the pair \uD83D\uDE00 produces 128512: four units form three stream elements. The code point count need not equal the number of user-perceived characters.

Examples

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String text = "A\uD83D\uDE00B";
        System.out.println(text.length());
        System.out.println(Arrays.toString(text.codePoints().toArray()));
    }
}

Result

4
[65, 128512, 66]

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources