liveBook · Manning

BreakIterator

As explained in 100 Java Mistakes and How to Avoid Them

BreakIterator is a Java text-processing API that determines locale-sensitive boundaries between units such as characters, words, lines, or sentences.

Treating a Java char as a complete character breaks on surrogate pairs and can split user-readable text incorrectly. BreakIterator.getCharacterInstance() finds boundaries that preserve surrogate pairs, combining character sequences, ligature clusters, and modifiers. section 6.1

Find character boundaries

BreakIterator iterator = BreakIterator.getCharacterInstance(); iterator.setText(string); iterator.first(); int from = iterator.next(rowLength * rowNumber); int to = iterator.next(rowLength - 1); section 6.1

Avoid character-processing failures by following these rules:

  • Use codepoint-aware int overloads for character classification instead of char overloads. section 6.1
  • Use special text APIs for user-readable text, and isolate text processing in utilities or reusable libraries such as ICU4J. section 6.1

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains getCharacterInstance() boundary handling and notes that iteration examines intervening characters rather than jumping directly.
Examples
Demonstrates getCharacterInstance(), setText(), iterator positioning, and boundary extraction for splitting text.
What you can do after
Apply it via BreakIterator.getCharacterInstance() to split user-readable text at character boundaries.
How it connects
Connects character boundaries with code points, surrogate pairs, combining character sequences, and ligature clusters.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.