public abstract class BreakIterator
extends Object implements Cloneable
| java.lang.Object | |
| java.text.BreakIterator | |
BreakIterator类实现了用于在文本中查找边界位置的方法。 BreakIterator实例维护当前位置并扫描返回出现边界的字符索引的文本。 在内部, BreakIterator扫描文本使用CharacterIterator ,并且因此能够扫描通过实现协议的任何对象保存文本。 使用StringCharacterIterator扫描String传递给setText对象。
您可以使用此类提供的工厂方法来创建各种类型的中断迭代器的实例。 尤其是,使用getWordInstance , getLineInstance , getSentenceInstance ,并getCharacterInstance创造BreakIterator s表示分别执行字,行,句子和字符边界分析。 单个BreakIterator只能在一个单元上工作(单词,行,句子等)。 对于您希望执行的每个单位边界分析,您必须使用不同的迭代器。
行边界分析确定了换行时文本字符串可以被破坏的位置。 该机制正确处理标点符号和带连字符的单词。 实际的换行需要考虑可用的行宽,并由更高级别的软件处理。
句子边界分析允许选择正确解释数字和缩写内的句号,以及尾标点符号(如引号和括号)。
搜索和替换功能以及文本编辑应用程序使用词边界分析,允许用户通过双击选择单词。 单词选择提供了对单词内部和后面的标点符号的正确解释。 不属于单词的字符(例如符号或标点符号)在双方都有分词符。
字符边界分析允许用户按照预期与角色进行交互,例如,在通过文本字符串移动光标时。 字符边界分析通过字符串提供正确的导航,而不管字符如何存储。 返回的边界可以是补充字符的边界,组合字符序列或连字集群。 例如,重音字符可能被存储为基本字符和变音符号。 用户认为是角色的语言可能会因语言而异。
BreakIterator的工厂方法返回的BreakIterator实例仅用于自然语言,不适用于编程语言文本。 然而,可以定义标记一种编程语言的子类。
例子 :
创建和使用文本边界:
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));
}
Find the next word:
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;
}
(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.)
也可以看看:
常量(Constants) |
|
|---|---|
int |
DONE 当达到第一个或最后一个文本边界时,DONE由previous(),next(),next(int),之前(int)和之后(int)返回。 |
Protected constructors |
|
|---|---|
BreakIterator() 构造函数。 |
|
公共方法(Public methods) |
|
|---|---|
Object |
clone() 创建此迭代器的副本 |
abstract int |
current() 返回next(),next(int),previous(),first(),last(),following(int)或之前(int)最近返回的文本边界的字符索引。 |
abstract int |
first() 返回第一个边界。 |
abstract int |
following(int offset) 返回指定字符偏移之后的第一个边界。 |
static Locale[] |
getAvailableLocales() 返回 |
static BreakIterator |
getCharacterInstance(Locale locale) 针对给定语言环境返回 |
static BreakIterator |
getCharacterInstance() 为 character breaks返回一个新的 |
static BreakIterator |
getLineInstance(Locale locale) 针对给定语言环境返回 |
static BreakIterator |
getLineInstance() 为 101669478766074返回 |
static BreakIterator |
getSentenceInstance(Locale locale) 针对给定语言环境,返回 |
static BreakIterator |
getSentenceInstance() 为 default locale返回 |
abstract CharacterIterator |
getText() 获取正在扫描的文本 |
static BreakIterator |
getWordInstance() 为 default locale返回 |
static BreakIterator |
getWordInstance(Locale locale) 针对给定语言环境返回 |
boolean |
isBoundary(int offset) 如果指定的字符偏移量是文本边界,则返回true。 |
abstract int |
last() 返回最后一个边界。 |
abstract int |
next() 返回当前边界之后的边界。 |
abstract int |
next(int n) 从当前边界返回第n个边界。 |
int |
preceding(int offset) 返回指定字符偏移量之前的最后一个边界。 |
abstract int |
previous() 返回当前边界之前的边界。 |
void |
setText(String newText) 设置一个新的文本字符串进行扫描。 |
abstract void |
setText(CharacterIterator newText) 设置一个新的文本进行扫描。 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
int DONE
当达到第一个或最后一个文本边界时,DONE由previous(),next(),next(int),之前(int)和之后(int)返回。
常量值:-1(0xffffffff)
int current ()
返回next(),next(int),previous(),first(),last(),following(int)或之前(int)最近返回的文本边界的字符索引。 如果这些方法中的任何一个返回BreakIterator.DONE因为已达到第一个或最后一个文本边界,则返回第一个或最后一个文本边界,具体取决于到达哪个边界。
| 返回(Returns) | |
|---|---|
int |
The text boundary returned from the above methods, first or last text boundary. |
int first ()
返回第一个边界。 迭代器的当前位置设置为第一个文本边界。
| 返回(Returns) | |
|---|---|
int |
The character index of the first text boundary. |
int following (int offset)
返回指定字符偏移之后的第一个边界。 如果指定的偏移量等于上一个文本边界,则返回BreakIterator.DONE ,并且迭代器的当前位置不变。 否则,迭代器的当前位置被设置为返回的边界。 返回的值总是大于偏移量或值BreakIterator.DONE 。
| 参数(Parameters) | |
|---|---|
offset |
int: the character offset to begin scanning. |
| 返回(Returns) | |
|---|---|
int |
The first boundary after the specified offset or BreakIterator.DONE if the last text boundary is passed in as the offset. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the specified offset is less than the first text boundary or greater than the last text boundary. |
Locale[] getAvailableLocales ()
返回get*Instance方法可返回本地化实例的所有语言环境的数组。 返回的数组表示Java运行时支持的语言环境的联合,以及已安装的BreakIteratorProvider实现。 它必须包含至少一个Locale情况下等于Locale.US 。
| 返回(Returns) | |
|---|---|
Locale[] |
An array of locales for which localized BreakIterator instances are available. |
BreakIterator getCharacterInstance (Locale locale)
针对给定语言环境,返回 BreakIterator的新实例 BreakIterator 。
| 参数(Parameters) | |
|---|---|
locale |
Locale: the desired locale |
| 返回(Returns) | |
|---|---|
BreakIterator |
A break iterator for character breaks |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if locale is null |
BreakIterator getCharacterInstance ()
为 default locale返回 BreakIterator的新实例 BreakIterator 。
| 返回(Returns) | |
|---|---|
BreakIterator |
A break iterator for character breaks |
BreakIterator getLineInstance (Locale locale)
针对给定语言环境,返回 BreakIterator的新实例 BreakIterator 。
| 参数(Parameters) | |
|---|---|
locale |
Locale: the desired locale |
| 返回(Returns) | |
|---|---|
BreakIterator |
A break iterator for line breaks |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if locale is null |
BreakIterator getLineInstance ()
为 default locale返回 BreakIterator的新实例 BreakIterator 。
| 返回(Returns) | |
|---|---|
BreakIterator |
A break iterator for line breaks |
BreakIterator getSentenceInstance (Locale locale)
针对给定语言环境返回 BreakIterator的新实例 BreakIterator 。
| 参数(Parameters) | |
|---|---|
locale |
Locale: the desired locale |
| 返回(Returns) | |
|---|---|
BreakIterator |
A break iterator for sentence breaks |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if locale is null |
BreakIterator getSentenceInstance ()
为 default locale返回 BreakIterator的新实例 BreakIterator 。
| 返回(Returns) | |
|---|---|
BreakIterator |
A break iterator for sentence breaks |
CharacterIterator getText ()
获取正在扫描的文本
| 返回(Returns) | |
|---|---|
CharacterIterator |
the text being scanned |
BreakIterator getWordInstance ()
为 default locale返回 BreakIterator的新实例 BreakIterator 。
| 返回(Returns) | |
|---|---|
BreakIterator |
A break iterator for word breaks |
BreakIterator getWordInstance (Locale locale)
针对给定语言环境返回 BreakIterator的新实例 BreakIterator 。
| 参数(Parameters) | |
|---|---|
locale |
Locale: the desired locale |
| 返回(Returns) | |
|---|---|
BreakIterator |
A break iterator for word breaks |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if locale is null |
boolean isBoundary (int offset)
如果指定的字符偏移量是文本边界,则返回true。
| 参数(Parameters) | |
|---|---|
offset |
int: the character offset to check. |
| 返回(Returns) | |
|---|---|
boolean |
true if "offset" is a boundary position, false otherwise. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the specified offset is less than the first text boundary or greater than the last text boundary. |
int last ()
返回最后一个边界。 迭代器的当前位置被设置为最后的文本边界。
| 返回(Returns) | |
|---|---|
int |
The character index of the last text boundary. |
int next ()
返回当前边界之后的边界。 如果当前边界是最后一个文本边界,则返回BreakIterator.DONE并且迭代器的当前位置不变。 否则,将迭代器的当前位置设置为当前边界之后的边界。
| 返回(Returns) | |
|---|---|
int |
The character index of the next text boundary or BreakIterator.DONE if the current boundary is the last text boundary. Equivalent to next(1). |
也可以看看:
int next (int n)
从当前边界返回第n个边界。 如果已达到第一个或最后一个文本边界,则返回BreakIterator.DONE ,并且当前位置根据所达到的是第一个还是最后一个文本边界。 否则,迭代器的当前位置被设置为新的边界。 例如,如果迭代器的当前位置是第m个文本边界,并且从当前边界到最后一个文本边界存在三个边界,则下一个(2)调用将返回m + 2.新文本位置设置为(m + 2)个文本边界。 下一个(4)调用将返回BreakIterator.DONE ,最后的文本边界将成为新的文本位置。
| 参数(Parameters) | |
|---|---|
n |
int: which boundary to return. A value of 0 does nothing. Negative values move to previous boundaries and positive values move to later boundaries. |
| 返回(Returns) | |
|---|---|
int |
The character index of the nth boundary from the current position or BreakIterator.DONE if either first or last text boundary has been reached. |
int preceding (int offset)
返回指定字符偏移量之前的最后一个边界。 如果指定的偏移量等于第一个文本边界,则返回BreakIterator.DONE ,并且迭代器的当前位置不变。 否则,迭代器的当前位置被设置为返回的边界。 返回的值总是小于偏移量或值BreakIterator.DONE 。
| 参数(Parameters) | |
|---|---|
offset |
int: the characater offset to begin scanning. |
| 返回(Returns) | |
|---|---|
int |
The last boundary before the specified offset or BreakIterator.DONE if the first text boundary is passed in as the offset. |
| 抛出异常(Throws) | |
|---|---|
IllegalArgumentException |
if the specified offset is less than the first text boundary or greater than the last text boundary. |
int previous ()
返回当前边界之前的边界。 如果当前边界是第一个文本边界,则返回BreakIterator.DONE ,并且迭代器的当前位置不变。 否则,迭代器的当前位置被设置为当前边界之前的边界。
| 返回(Returns) | |
|---|---|
int |
The character index of the previous text boundary or BreakIterator.DONE if the current boundary is the first text boundary. |
void setText (String newText)
设置一个新的文本字符串进行扫描。 当前扫描位置重置为第一个()。
| 参数(Parameters) | |
|---|---|
newText |
String: new text to scan. |
void setText (CharacterIterator newText)
设置一个新的文本进行扫描。 当前扫描位置重置为第一个()。
| 参数(Parameters) | |
|---|---|
newText |
CharacterIterator: new text to scan. |