A codepoint-centered API processes text as Unicode code points instead of individual UTF-16 char values. This avoids misreading characters outside the Basic Multilingual Plane, which may occupy surrogate pairs. Prefer String methods such as codePointAt(), codePointBefore(), codePointCount(), and codePoints(), plus StringBuilder.appendCodePoint(). section 6.1
Replace char-based digit checks
A char-based check fails for a digit represented outside the Basic Multilingual Plane: static boolean startsWithDigit(String str) { return !str.isEmpty() && Character.isDigit(str.charAt(0)); } Use the int overload with a code point instead: static boolean startsWithDigit(String str) { return !str.isEmpty() && Character.isDigit(str.codePointAt(0)); } The int overload handles characters that char-based methods cannot represent. section 6.1
String part = string.codePoints().skip(rowNumber * rowLength)
.limit(rowLength)
.collect(StringBuilder::new,
StringBuilder::appendCodePoint,
StringBuilder::append)
.toString();
Extracts a string portion by traversing code points and rebuilding it with StringBuilder.appendCodePoint(). section 6.1
Use the technique with these cautions:
- Avoid char-accepting APIs in public interfaces when arbitrary characters are possible. section 6.1
- Remember that codePointAt(index) accepts a char index, not an ordinal codepoint position. section 6.1
- Expect more verbose traversal and potentially slower nth-codepoint access than charAt() or substring(). section 6.1
