BreakIterator class implements methods for finding
the location of boundaries in text. Instances of BreakIterator
maintain a current position and scan over text
returning the index of characters where boundaries occur.
Internally, BreakIterator scans text using a
CharacterIterator, and is thus able to scan text held
by any object implementing that protocol. A StringCharacterIterator
is used to scan String objects passed to setText.
You use the factory methods provided by this class to create
instances of various types of break iterators. In particular,
use getWordInstance, getLineInstance,
getSentenceInstance, and getCharacterInstance
to create BreakIterators that perform
word, line, sentence, and character boundary analysis respectively.
A single BreakIterator can work only on one unit
(word, line, sentence, and so on). You must use a different iterator
for each unit boundary analysis you wish to perform.
Line boundary analysis determines where a text string can be broken when line-wrapping. The mechanism correctly handles punctuation and hyphenated words. Actual line breaking needs to also consider the available line width and is handled by higher-level software.
Sentence boundary analysis allows selection with correct interpretation of periods within numbers and abbreviations, and trailing punctuation marks such as quotation marks and parentheses.
Word boundary analysis is used by search and replace functions, as well as within text editing applications that allow the user to select words with a double click. Word selection provides correct interpretation of punctuation marks within and following words. Characters that are not part of a word, such as symbols or punctuation marks, have word-breaks on both sides.
Character boundary analysis allows users to interact with characters as they expect to, for example, when moving the cursor through a text string. Character boundary analysis provides correct navigation through character strings, regardless of how the character is stored. The boundaries returned may be those of supplementary characters, combining character sequences, or ligature clusters. For example, an accented character might be stored as a base character and a diacritical mark. What users consider to be a character can differ between languages.
Implementation Specification
The default implementation of the character boundary analysis conforms to the Unicode Consortium's Extended Grapheme Cluster breaks. For more detail, refer to Grapheme Cluster Boundaries section in the Unicode Standard Annex #29.
Implementation Note
The default implementations of BreakIterator will perform the equivalent
of calling setText("") if the text hasn't been set by either
setText(String) or setText(CharacterIterator)
and a boundary searching operation is called by the BreakIterator instance.
The BreakIterator instances returned by the factory methods
of this class are intended for use with natural languages only, not for
programming language text. It is however possible to define subclasses
that tokenize a programming language.
Examples:
Creating and using text boundaries:
Print each element in order:public static void main(String args[]) { if (args.length == 1) { String stringToExamine = args[0]; //print each word in order BreakIterator boundary = BreakIterator.getWordInstance(); boundary.setText(stringToExamine); printEachForward(boundary, stringToExamine); //print each sentence in reverse order boundary = BreakIterator.getSentenceInstance(Locale.US); boundary.setText(stringToExamine); printEachBackward(boundary, stringToExamine); printFirst(boundary, stringToExamine); printLast(boundary, stringToExamine); } }
Print each element in reverse order:public static void printEachForward(BreakIterator boundary, String source) { int start = boundary.first(); for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) { System.out.println(source.substring(start,end)); } }
Print first element:public static void printEachBackward(BreakIterator boundary, String source) { int end = boundary.last(); for (int start = boundary.previous(); start != BreakIterator.DONE; end = start, start = boundary.previous()) { System.out.println(source.substring(start,end)); } }
Print last element:public static void printFirst(BreakIterator boundary, String source) { int start = boundary.first(); int end = boundary.next(); System.out.println(source.substring(start,end)); }
Print the element at a specified position:public static void printLast(BreakIterator boundary, String source) { int end = boundary.last(); int start = boundary.previous(); System.out.println(source.substring(start,end)); }
Find the next word:public static void printAt(BreakIterator boundary, int pos, String source) { int end = boundary.following(pos); int start = boundary.previous(); System.out.println(source.substring(start,end)); }
(The iterator returned by BreakIterator.getWordInstance() is unique in that the break positions it returns don't represent both the start and end of the thing being iterated over. That is, a sentence-break iterator returns breaks that each represent the end of one sentence and the beginning of the next. With the word-break iterator, the characters between two boundaries might be a word, or they might be the punctuation or whitespace between two words. The above code uses a simple heuristic to determine which boundary is the beginning of a word: If the characters between this boundary and the next boundary include at least one letter (this can be an alphabetical letter, a CJK ideograph, a Hangul syllable, a Kana character, etc.), then the text between this boundary and the next is a word; otherwise, it's the material between words.)public static int nextWordStartAfter(int pos, String text) { BreakIterator wb = BreakIterator.getWordInstance(); wb.setText(text); int last = wb.following(pos); int current = wb.next(); while (current != BreakIterator.DONE) { for (int p = last; p < current; p++) { if (Character.isLetter(text.codePointAt(p))) return last; } last = current; current = wb.next(); } return BreakIterator.DONE; }
CharacterIterator
| Modifier and Type | Class and Description |
|---|---|
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private static final int | |
| public static final int | DONE
DONE is returned by previous(), next(), next(int), preceding(int) and following(int) when either the first or last text boundary has been reached. |
| private static final SoftReference | |
| private static final int | |
| private static final int | |
| private static final int |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public Object | |
| private static BreakIterator | |
| private static BreakIterator | |
| public abstract int | Returns: The text boundary returned from the above methods, first or last text boundary.Returns character index of the text boundary that was most recently returned by next(), next(int), previous(), first(), last(), following(int) or preceding(int). |
| public abstract int | |
| public abstract int | Returns: The first boundary after the specified offset orBreakIterator.DONE if the last text boundary is passed in
as the offset.the character offset to begin scanning. offset)Returns the first boundary following the specified character offset. |
| public static synchronized Locale[] | Returns: An array of locales for which localizedBreakIterator instances are available.Returns an array of all locales for which the
|
| private static BreakIterator | |
| public static BreakIterator | Returns: A break iterator for character breaksReturns a new |
| public static BreakIterator | Returns: A break iterator for character breaksthe desired locale locale)Returns a new |
| public static BreakIterator | Returns: A break iterator for line breaksReturns a new |
| public static BreakIterator | Returns: A break iterator for line breaksthe desired locale locale)Returns a new |
| public static BreakIterator | Returns: A break iterator for sentence breaksReturns a new |
| public static BreakIterator | Returns: A break iterator for sentence breaksthe desired locale locale)Returns a new |
| public abstract CharacterIterator | |
| public static BreakIterator | Returns: A break iterator for word breaksReturns a new |
| public static BreakIterator | Returns: A break iterator for word breaksthe desired locale locale)Returns a new |
| public boolean | Returns: true if "offset" is a boundary position,
false otherwise.the character offset to check. offset)Returns true if the specified character offset is a text boundary. |
| public abstract int | |
| public abstract int | Returns: The character index of the nth boundary from the current position orBreakIterator.DONE if either first or last text boundary
has been reached.which boundary to return. A value of 0
does nothing. Negative values move to previous boundaries
and positive values move to later boundaries. n)Returns the nth boundary from the current boundary. |
| public abstract int | Returns: The character index of the next text boundary orBreakIterator.DONE if the current boundary is the last text
boundary.
Equivalent to next(1).Returns the boundary following the current boundary. |
| public int | Returns: The last boundary before the specified offset orBreakIterator.DONE if the first text boundary is passed in
as the offset.the character offset to begin scanning. offset)Returns the last boundary preceding the specified character offset. |
| public abstract int | Returns: The character index of the previous text boundary orBreakIterator.DONE if the current boundary is the first text
boundary.Returns the boundary preceding the current boundary. |
| public void | |
| public abstract void |
| CHARACTER_INDEX | back to summary |
|---|---|
| private static final int CHARACTER_INDEX | |
| DONE | back to summary |
|---|---|
| public static final int DONE DONE is returned by previous(), next(), next(int), preceding(int) and following(int) when either the first or last text boundary has been reached. | |
| iterCache | back to summary |
|---|---|
private static final SoftReference<BreakIterator.
| |
| LINE_INDEX | back to summary |
|---|---|
| private static final int LINE_INDEX | |
| SENTENCE_INDEX | back to summary |
|---|---|
| private static final int SENTENCE_INDEX | |
| WORD_INDEX | back to summary |
|---|---|
| private static final int WORD_INDEX | |
| BreakIterator | back to summary |
|---|---|
| protected BreakIterator() Constructor. BreakIterator is stateless and has no default behavior. | |
| clone | back to summary |
|---|---|
| public Object clone() Overrides java. Create a copy of this iterator | |
| createBreakInstance | back to summary |
|---|---|
| private static BreakIterator createBreakInstance(Locale locale, int type) | |
| createBreakInstance | back to summary |
|---|---|
| private static BreakIterator createBreakInstance(LocaleProviderAdapter adapter, Locale locale, int type) | |
| current | back to summary |
|---|---|
| public abstract int current() Returns character index of the text boundary that was most
recently returned by next(), next(int), previous(), first(), last(),
following(int) or preceding(int). If any of these methods returns
| |
| first | back to summary |
|---|---|
| public abstract int first() Returns the first boundary. The iterator's current position is set to the first text boundary.
| |
| following | back to summary |
|---|---|
| public abstract int following(int offset) Returns the first boundary following the specified character offset. If the
specified offset is equal to the last text boundary, it returns
| |
| getAvailableLocales | back to summary |
|---|---|
| public static synchronized Locale[] getAvailableLocales() Returns an array of all locales for which the
| |
| getBreakInstance | back to summary |
|---|---|
| private static BreakIterator getBreakInstance(Locale locale, int type) | |
| getCharacterInstance | back to summary |
|---|---|
| public static BreakIterator getCharacterInstance() Returns a new
| |
| getCharacterInstance | back to summary |
|---|---|
| public static BreakIterator getCharacterInstance(Locale locale) Returns a new
| |
| getLineInstance | back to summary |
|---|---|
| public static BreakIterator getLineInstance() Returns a new
| |
| getLineInstance | back to summary |
|---|---|
| public static BreakIterator getLineInstance(Locale locale) Returns a new
| |
| getSentenceInstance | back to summary |
|---|---|
| public static BreakIterator getSentenceInstance() Returns a new
| |
| getSentenceInstance | back to summary |
|---|---|
| public static BreakIterator getSentenceInstance(Locale locale) Returns a new
| |
| getText | back to summary |
|---|---|
| public abstract CharacterIterator getText() Get the text being scanned
| |
| getWordInstance | back to summary |
|---|---|
| public static BreakIterator getWordInstance() Returns a new
| |
| getWordInstance | back to summary |
|---|---|
| public static BreakIterator getWordInstance(Locale locale) Returns a new
| |
| isBoundary | back to summary |
|---|---|
| public boolean isBoundary(int offset) Returns true if the specified character offset is a text boundary.
| |
| last | back to summary |
|---|---|
| public abstract int last() Returns the last boundary. The iterator's current position is set to the last text boundary.
| |
| next | back to summary |
|---|---|
| public abstract int next(int n) Returns the nth boundary from the current boundary. If either
the first or last text boundary has been reached, it returns
| |
| next | back to summary |
|---|---|
| public abstract int next() Returns the boundary following the current boundary. If the current boundary
is the last text boundary, it returns
| |
| preceding | back to summary |
|---|---|
| public int preceding(int offset) Returns the last boundary preceding the specified character offset. If the
specified offset is equal to the first text boundary, it returns
| |
| previous | back to summary |
|---|---|
| public abstract int previous() Returns the boundary preceding the current boundary. If the current boundary
is the first text boundary, it returns
| |
| setText | back to summary |
|---|---|
| public void setText(String newText) Set a new text string to be scanned. The current scan position is reset to first().
| |
| setText | back to summary |
|---|---|
| public abstract void setText(CharacterIterator newText) Set a new text for scanning. The current scan position is reset to first().
| |
| Modifier and Type | Field and Description |
|---|---|
| private BreakIterator | |
| private Locale |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv BreakIterator | |
| pack-priv Locale |
| iter | back to summary |
|---|---|
| private BreakIterator iter | |
| locale | back to summary |
|---|---|
| private Locale locale | |
| BreakIteratorCache | back to summary |
|---|---|
| pack-priv BreakIteratorCache(Locale locale, BreakIterator iter) | |
| createBreakInstance | back to summary |
|---|---|
| pack-priv BreakIterator createBreakInstance() | |
| getLocale | back to summary |
|---|---|
| pack-priv Locale getLocale() | |