public abstract class BreakIterator extends Object implements Cloneable
BreakIterator
类实现的方法找到文本边界的位置。实例
BreakIterator
保持当前位置和扫描文本返回在边界发生性状指标。在内部,
BreakIterator
扫描文本使用
CharacterIterator
,因而能够扫描文本的任何对象实现该协议举行。一个
StringCharacterIterator
用来扫描
String
对象传递给
setText
。
你使用这个类提供的工厂方法来创建各种类型的迭代器实例打破。特别是,使用getWordInstance
,getLineInstance
,getSentenceInstance
,getCharacterInstance
创建和执行的话,线,句子BreakIterator
s,和字符边界分析。一个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); } }
打印每个元素以相反的顺序: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)); } }
打印第一要素: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)); } }
打印最后一个元素:z-95f8d1e3-899b-487f- 90ae-68eb703461be打印在指定位置的元素:public static void printFirst(BreakIterator boundary, String source) { int start = boundary.first(); int end = boundary.next(); System.out.println(source.substring(start,end)); }
找出下一个词: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 | Field and Description |
---|---|
static int |
DONE
做的是通过previous(),next()回来了,下一个(int),前(int)及以下(int)时,第一个和最后一个文本边界已经达到。
|
Modifier | Constructor and Description |
---|---|
protected |
BreakIterator()
构造函数。
|
Modifier and Type | Method and Description |
---|---|
Object |
clone()
创建此迭代器的副本
|
abstract int |
current()
文本的边界,最近next()返回返回的字符索引,下一个(int),previous(),first(),last(),以下(int)或前(int)。
|
abstract int |
first()
返回第一个边界。
|
abstract int |
following(int offset)
返回指定字符偏移量的第一个边界。
|
static Locale[] |
getAvailableLocales()
返回一个数组的所有地方,这个班的
get*Instance 方法可以返回局部实例。
|
static BreakIterator |
getCharacterInstance()
|
static BreakIterator |
getCharacterInstance(Locale locale)
返回给现场
character breaks新
BreakIterator 实例。
|
static BreakIterator |
getLineInstance()
|
static BreakIterator |
getLineInstance(Locale locale)
返回给现场
line breaks新
BreakIterator 实例。
|
static BreakIterator |
getSentenceInstance()
|
static BreakIterator |
getSentenceInstance(Locale locale)
返回给现场
sentence breaks新
BreakIterator 实例。
|
abstract CharacterIterator |
getText()
获取正在被扫描的文本
|
static BreakIterator |
getWordInstance()
|
static BreakIterator |
getWordInstance(Locale locale)
返回给现场
word breaks新
BreakIterator 实例。
|
boolean |
isBoundary(int offset)
如果指定的字符偏移量是一个文本边界,则返回真。
|
abstract int |
last()
返回最后一个边界。
|
abstract int |
next()
返回当前边界的边界。
|
abstract int |
next(int n)
从目前的边界返回n边。
|
int |
preceding(int offset)
返回指定字符偏移量的最后一个边界。
|
abstract int |
previous()
返回当前边界的边界。
|
abstract void |
setText(CharacterIterator newText)
为扫描设置一个新的文本。
|
void |
setText(String newText)
设置要扫描的新文本字符串。
|
public static final int DONE
public abstract int first()
public abstract int last()
public abstract int next(int n)
BreakIterator.DONE
和当前位置设置为第一个或最后一个文本的边界取决于哪一个是达到。否则,迭代器的当前位置将被设置为新的边界。例如,如果对象的当前位置是M文本边界三边界存在从当前边界的最后文本边界,下(2)调用将返回M + 2。将新的文本位置设置为(M + 2)第(M +)文本边界。下一个(4)调用将返回
BreakIterator.DONE
和最后文本边界将成为新的文本位置。
n
-返回的边界。一个0的价值什么都没有。负值移动到以前的边界和积极的值移动到以后的边界。
BreakIterator.DONE
如果第一个或最后一个文本边界已经达到。
public abstract int next()
BreakIterator.DONE
和迭代器的当前位置是不变的。否则,迭代器的当前位置将被设置为当前边界的边界。
BreakIterator.DONE
指数如果当前边界是最后文本边界。相当于下一个(1)。
next(int)
public abstract int previous()
BreakIterator.DONE
和迭代器的当前位置是不变的。否则,迭代器的当前位置设置为当前边界的前面的边界。
BreakIterator.DONE
特征指标如果当前边界是第一个文本边界。
public abstract int following(int offset)
BreakIterator.DONE
和迭代器的当前位置是不变的。否则,迭代器的当前位置设置为返回的边界。返回的值总是大于偏移量或价值
BreakIterator.DONE
。
offset
的字符偏移量开始扫描。
BreakIterator.DONE
如果最后文本边界是通过在偏移后的第一个边界。
IllegalArgumentException
-如果指定的偏移量小于第一文本边界或大于最后文本边界。
public int preceding(int offset)
BreakIterator.DONE
和迭代器的当前位置是不变的。否则,迭代器的当前位置设置为返回的边界。返回的值总是小于偏移量或价值
BreakIterator.DONE
。
offset
的字符偏移量开始扫描。
BreakIterator.DONE
如果第一文本的边界是通过作为偏移量。
IllegalArgumentException
-如果指定的偏移量小于第一文本边界或大于最后文本边界。
public boolean isBoundary(int offset)
offset
的字符偏移量检查。
true
如果“补偿”是一个边界的位置,
false
否则。
IllegalArgumentException
-如果指定的偏移量小于第一文本边界或大于最后文本边界。
public abstract int current()
BreakIterator.DONE
因为第一个或最后一个文本边界已经达到,则返回第一个或最后一个文本的边界取决于哪一个是达到。
next()
,
next(int)
,
previous()
,
first()
,
last()
,
following(int)
,
preceding(int)
public abstract CharacterIterator getText()
public void setText(String newText)
newText
-扫描新的文本。
public abstract void setText(CharacterIterator newText)
newText
-扫描新的文本。
public static BreakIterator getWordInstance()
public static BreakIterator getWordInstance(Locale locale)
BreakIterator
实例。
locale
-所需的现场
NullPointerException
-如果
locale
是空的
public static BreakIterator getLineInstance()
public static BreakIterator getLineInstance(Locale locale)
BreakIterator
实例。
locale
-所需的现场
NullPointerException
-如果
locale
是空的
public static BreakIterator getCharacterInstance()
public static BreakIterator getCharacterInstance(Locale locale)
BreakIterator
实例。
locale
-所需的现场
NullPointerException
-如果
locale
是空的
public static BreakIterator getSentenceInstance()
public static BreakIterator getSentenceInstance(Locale locale)
BreakIterator
实例。
locale
-所需的现场
NullPointerException
-如果
locale
是空的
public static Locale[] getAvailableLocales()
get*Instance
方法可以返回局部实例。返回的数组是由java运行时支持的地区联盟,通过安装
BreakIteratorProvider
实现。它必须包含至少一个
Locale
实例等于
Locale.US
。
BreakIterator
实例可用数组。
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.