I can iterate through the codepoints of a string using either of the following two ways:
final int length = s.length();
for (int offset = 0; offset < length; ) {
final int codepoint = s.codePointAt(offset);
// do something with the codepoint
offset += Character.charCount(codepoint);
}
BreakIterator.getCharacterInstance()
BreakIterator boundary = BreakIterator.getCharacterInstance();
boundary.setText(s);
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; end = boundary.next()) {
int codepoint = text.substring(start, end);
// do something with the codepoint
start = end;
}
From a performance perspective, is one any better than the other? I need to iterate through codepoints in a custom view and performance is a factor.
Update
As was noted in the comments, it would be good to include CharSequence.codePoints()
, which is an IntStream
. However, I am struggling to understand how streams work. Currently I have read this, this, and this. This probably deserves a new question.
I am also adding the Android tag, since I am doing this in Android and IntStream
was not added until API 24.