1

I can iterate through the codepoints of a string using either of the following two ways:

String.codePointAt

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.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • Starting from Java 8 there is [another way](http://docs.oracle.com/javase/8/docs/api/java/lang/CharSequence.html#codePoints--) that uses Streams, maybe add that one too? – Todd Sewell Feb 15 '17 at 08:04
  • @ToddSewell, That's a new one to me. Thanks for letting me know. – Suragch Feb 15 '17 at 08:06

0 Answers0