Sub topic 12
Unicode Code Points and Text Boundaries
A Unicode code point is a number that identifies an element of text, such as A or an emoji. Unicode is the standard that assigns these numbers.
Java strings use UTF-16, a way to represent text as 16-bit units. A code unit is one of those units. A Java char holds one UTF-16 code unit, not necessarily a whole code point.
Most common characters fit in one code unit. Some code points, including many emoji, need two. Those two units form a surrogate pair.
Length Is Not Always a Code Point Count
Consider A followed by the emoji 😀. The A uses one code unit, and the emoji uses two. So the string has three code units but two code points.
The position between the two halves of the pair is not a code point boundary. Extracting just one half does not give you a complete emoji.
Count, Visit, and Extract Code Points
Save this program as UnicodeText.java:
class UnicodeText {
public static void main(String[] args) {
String text = "A\uD83D\uDE00";
System.out.println("Code units: " + text.length());
System.out.println("Code points: "
+ text.codePointCount(0, text.length()));
for (int index = 0; index < text.length();) {
int point = text.codePointAt(index);
System.out.printf("U+%04X%n", point);
index += Character.charCount(point);
}
int start = text.offsetByCodePoints(0, 1);
int end = text.offsetByCodePoints(0, 2);
System.out.println("Second code point: "
+ text.substring(start, end));
}
}Compile and run it:
javac UnicodeText.java
java UnicodeTextThe output is:
Code units: 3
Code points: 2
U+0041
U+1F600
Second code point: 😀The emoji display depends on your terminal and its font.
Here is what the program does:
length()counts UTF-16 code units.codePointCount(0, text.length())counts code points across the whole string.codePointAt(index)reads the code point starting at a code-unit index. The loop then moves forward byCharacter.charCount(point), which is one or two for a valid code point.offsetByCodePoints(0, 1)moves one code point from index0, giving index1. Moving two gives index3.substring(1, 3)extracts the emoji without splitting its pair.
A Visible Character Can Have Several Code Points
Even a code point count is not always a count of visible characters. For example, e followed by a combining mark—a code point that adds a mark to another character—can appear as one accented letter. It still contains two code points.
A text boundary is a position between parts of text. When you need boundaries between visible characters, such as for moving a cursor, counting code points alone may not be enough. Java provides BreakIterator.getCharacterInstance() as a starting point. Test it with the text and Java version you expect, especially for complex emoji.
Practice
Exercise
In the example string, what indexes should you pass to substring to extract the emoji, and why?
Show answer
Use substring(1, 3). The emoji occupies code units at indexes 1 and 2, and the end index is exclusive.
Exercise
Can two code points appear as one visible character?
Show answer
Yes. For example, an e followed by a combining accent can appear as one accented letter.