| Contents | Package | Class | Tree | Deprecated | Index | Help | Java 1.2 Beta 3 | ||
| PREV | NEXT | SHOW LISTS | HIDE LISTS | ||
java.lang.Object
|
+----java.text.BreakIterator
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 getWordIterator, getLineIterator,
getSentenceIterator, and getCharacterIterator
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.
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 of through character strings, regardless of how the character is stored. 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.
BreakIterator is intended for use with natural
languages only. Do not use this class to tokenize a programming language.
Examples:
Creating and using text boundaries
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 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 each element in reverse order
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 first 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 last element
public static void printLast(BreakIterator boundary, String source) {
int end = boundary.last();
int start = boundary.previous();
System.out.println(source.substring(start,end));
}
Print the element at a specified position
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));
}
| Field Summary | |
| static int | DONE
|
| Constructor Summary | |
| BreakIterator()
|
|
| Method Summary | |
| Object | clone()
|
| int | current()
|
| int | first()
|
| int | following(int offset)
|
| static Locale[] | getAvailableLocales()
|
| static BreakIterator | getCharacterInstance()
|
| static BreakIterator | getCharacterInstance(Locale where)
|
| static BreakIterator | getLineInstance()
|
| static BreakIterator | getLineInstance(Locale where)
|
| static BreakIterator | getSentenceInstance()
|
| static BreakIterator | getSentenceInstance(Locale where)
|
| CharacterIterator | getText()
|
| static BreakIterator | getWordInstance()
|
| static BreakIterator | getWordInstance(Locale where)
|
| int | last()
|
| int | next(int n)
|
| int | next()
|
| int | previous()
|
| void | setText(String newText)
|
| void | setText(CharacterIterator newText)
|
| Methods inherited from class java.lang.Object |
| clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public static final int DONE
| Constructor Detail |
protected BreakIterator()
| Method Detail |
public Object clone()
public abstract int first()
public abstract int last()
public abstract int next(int n)
n
- which boundary to return. A value of 0
does nothing. Negative values move to previous boundaries
and positive values move to later boundaries.
public abstract int next()
public abstract int previous()
public abstract int following(int offset)
offset
- the offset to begin scanning. Valid values
are determined by the CharacterIterator passed to
setText(). Invalid values cause
an IllegalArgumentException to be thrown.
public abstract int current()
public abstract CharacterIterator getText()
public void setText(String newText)
newText
- new text to scan.
public abstract void setText(CharacterIterator newText)
newText
- new text to scan.
public static BreakIterator getWordInstance()
public static BreakIterator getWordInstance(Locale where)
where
- the local. If a specific WordBreak is not
avaliable for the specified locale, a default WordBreak is returned.
public static BreakIterator getLineInstance()
public static BreakIterator getLineInstance(Locale where)
where
- the local. If a specific LineBreak is not
avaliable for the specified locale, a default LineBreak is returned.
public static BreakIterator getCharacterInstance()
public static BreakIterator getCharacterInstance(Locale where)
where
- the local. If a specific character break is not
avaliable for the specified local, a default character break is returned.
public static BreakIterator getSentenceInstance()
public static BreakIterator getSentenceInstance(Locale where)
where
- the local. If a specific SentenceBreak is not
avaliable for the specified local, a default SentenceBreak is returned.
public static Locale[] getAvailableLocales()
| Contents | Package | Class | Tree | Deprecated | Index | Help | Java 1.2 Beta 3 | ||
| PREV | NEXT | SHOW LISTS | HIDE LISTS | ||