I'm making my own text processor in Android (a custom vertical script TextView for Mongolian). I thought I would have to find all the line breaking locations myself so that I could implement line wrapping, but then I discovered BreakIterator
. This seems to find all the possible breaks between characters, words, lines, and sentences in various languages.
I'm trying to learn how to use it. The documentation was more helpful than average, but it was still difficult to understand from just reading. I also found a few tutorials (see here, here, and here) but they lacked the full explanation with output that I was looking for.
I am adding this Q&A style answer to help myself learn how to use BreakIterator
.
I'm making this an Android tag in addition to Java because there is apparently some difference between them. Also, Android now supports the ICU BreakIterator
and future answers may deal with this.
BreakIterator
can be used to find the possible breaks between characters, words, lines, and sentences. This is useful for things like moving the cursor through visible characters, double clicking to select words, triple clicking to select sentences, and line wrapping.Boilerplate code
The following code is used in the examples below. Just adjust the first part to change the text and type of
BreakIterator
.If you just want to test this out, you can paste it directly into an Activity's
onCreate
in Android. I'm usingSystem.out.println
rather thanLog
so that it is also testable in a Java only environment.I'm using the
java.text.BreakIterator
rather than the ICU one, which is only available from API 24. See the links at the bottom for more information.Characters
Change the boilerplate code to include the following
Output