|
JavaTM 2 Platform Standard Ed. 5.0 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
java.lang.Objectjava.text.BreakIterator
public abstract class BreakIterator
BreakIterator 类实现了用于查找文本中边界位置的方法。BreakIterator 的实例维护一个当前位置,并在文本上扫描返回的边界出现的字符索引。在内部,BreakIterator 使用 CharacterIterator 扫描文本,因此能扫描实现该协议的任何对象所保存的文本。StringCharacterIterator 用于扫描传递给 setText 的 String 对象。
可以用此类提供的工厂方法来创建分解迭代器的不同类型的实例。尤其是,使用 getWordIterator、getLineIterator、getSentenceIterator 和 getCharacterIterator 分别创建执行单词、行、句子和字符边界分析的 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);
}
}
按顺序打印每个元素
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));
}
}
打印第一个元素
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 printLast(BreakIterator boundary, String source) {
int end = boundary.last();
int start = boundary.previous();
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));
}
查找下一个单词
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;
}
(BreakIterator.getWordInstance() 所返回的迭代器是惟一的,在其中,它所返回的分解位置不表示迭代内容的开始和结束。也就是说,一个句子分解迭代器返回的每一个分解表示一个句子的结束和下一个句子的开始。用单词分解迭代器,两个边界之间的字符可能是一个单词,也可能是两个单词之间的标点符号或空白。上面的代码使用一个简单的探索来确定哪一个边界是单词的开始:如果此边界和下一边界之间的字符至少包含了一个字母(这可以是字母表中的字母、中日韩 (CJK) 表意字符、韩文音节、日语假名字符等等),那么此边界和下一边界之间的文本就是一个单词;否则,它就是单词之间的内容。)
CharacterIterator| 字段摘要 | |
|---|---|
static int |
DONE
当所有有效边界返回之后,previous() 和 next() 返回 DONE。 |
| 构造方法摘要 | |
|---|---|
protected |
BreakIterator()
构造方法。 |
| 方法摘要 | |
|---|---|
Object |
clone()
创建此迭代器的副本。 |
abstract int |
current()
返回最近由 next()、previous()、first() 或者 last() 返回的文本边界的字符索引。 |
abstract int |
first()
返回第一个边界。 |
abstract int |
following(int offset)
返回指定偏移量后面的第一个边界。 |
static Locale[] |
getAvailableLocales()
返回一个所有语言环境的数组,此类的 getInstance 方法可为这些语言环境返回已本地化的实例。 |
static BreakIterator |
getCharacterInstance()
使用默认语言环境为字符分解创建 BreakIterator,返回一个实现字符分解的 BreakIterator 实例。 |
static BreakIterator |
getCharacterInstance(Locale where)
使用默认语言环境为字符分解创建 BreakIterator,返回一个实现字符分解的 BreakIterator 实例。 |
protected static int |
getInt(byte[] buf,
int offset)
|
static BreakIterator |
getLineInstance()
使用默认语言环境为行分解创建 BreakIterator。 |
static BreakIterator |
getLineInstance(Locale where)
使用指定语言环境为行分解创建 BreakIterator。 |
protected static long |
getLong(byte[] buf,
int offset)
|
static BreakIterator |
getSentenceInstance()
使用默认语言环境为句子分解创建 BreakIterator,返回一个实现句子分解的 BreakIterator 实例。 |
static BreakIterator |
getSentenceInstance(Locale where)
使用默认语言环境为句子分解创建 BreakIterator,返回一个实现句子分解的 BreakIterator 实例。 |
protected static short |
getShort(byte[] buf,
int offset)
|
abstract CharacterIterator |
getText()
获取正被扫描的文本 |
static BreakIterator |
getWordInstance()
使用默认语言环境为单词分解创建 BreakIterator。 |
static BreakIterator |
getWordInstance(Locale where)
使用指定语言环境为单词分解创建 BreakIterator。 |
boolean |
isBoundary(int offset)
如果指定位置是一个边界位置,则返回 true。 |
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)
设置要被扫描的新文本字符串。 |
| 从类 java.lang.Object 继承的方法 |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| 字段详细信息 |
|---|
public static final int DONE
| 构造方法详细信息 |
|---|
protected BreakIterator()
| 方法详细信息 |
|---|
public Object clone()
Object 中的 cloneCloneablepublic abstract int first()
public abstract int last()
public abstract int next(int n)
n - 要返回的边界。0 值表示无任何操作。负值移向前面的边界,而正值移向后面的边界。
public abstract int next()
public abstract int previous()
public abstract int following(int offset)
offset - 开始扫描的偏移量。有效值由传递给 setText() 的 CharacterIterator 定义。无效值导致抛出 IllegalArgumentException。
public int preceding(int offset)
offset - 开始扫描的偏移量。有效值由传递给 setText() 的 CharacterIterator 定义。无效值导致抛出 IllegalArgumentException。
public boolean isBoundary(int offset)
offset - 要检查的偏移量。
public abstract int current()
public abstract CharacterIterator getText()
public void setText(String newText)
newText - 要扫描的新文本。public abstract void setText(CharacterIterator newText)
newText - 要扫描的新文本。public static BreakIterator getWordInstance()
Locale.getDefault()public static BreakIterator getWordInstance(Locale where)
where - 语言环境。如果对于指定的语言环境来说特定的 WordBreak 不可用,则返回默认的 WordBreak。
public static BreakIterator getLineInstance()
Locale.getDefault()public static BreakIterator getLineInstance(Locale where)
where - 语言环境。如果对于指定的语言环境来说特定的 LineBreak 不可用,则返回默认的 LineBreak。
public static BreakIterator getCharacterInstance()
Locale.getDefault()public static BreakIterator getCharacterInstance(Locale where)
where - 语言环境。如果对于指定的语言环境来说特定的字符分解不可用,则返回默认的字符分解。
public static BreakIterator getSentenceInstance()
Locale.getDefault()public static BreakIterator getSentenceInstance(Locale where)
where - 语言环境。如果对于指定的语言环境来说特定的 SentenceBreak 不可用,则返回默认的 SentenceBreak。
public static Locale[] getAvailableLocales()
getInstance 方法可为这些语言环境返回已本地化的实例。返回的数组至少必须包含一个 Locale 实例,它等同于 Locale.US。
BreakIterator 实例是可用的。
protected static long getLong(byte[] buf,
int offset)
protected static int getInt(byte[] buf,
int offset)
protected static short getShort(byte[] buf,
int offset)
|
JavaTM 2 Platform Standard Ed. 5.0 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。