示例,共同学习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)); }
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.charAt(p)) return last; } last = current; current = wb.next(); } return BreakIterator.DONE; }
刚写的一个简单的例子 用 getWordInstance 举例 其他的是一样的import java.text.*;public class BreakIteratorTest{ public static void main(String[] args) throws Exception{ String str = "The BreakIterator class implements methods for finding the location of boundaries in text."; BreakIterator bi = BreakIterator.getWordInstance(); bi.setText(str); int next = bi.next(); System.out.println(next); System.out.println(str.charAt(next-1)); System.out.println(str.substring(0,next)); // The System.out.println(bi.isBoundary(2)); //false System.out.println(bi.isBoundary(3)); //true System.out.println("================================="); // Print all the elements int start = bi.first(); for (int end = bi.next(); end != BreakIterator.DONE; start = end, end = bi.next()) { System.out.println(str.substring(start,end)); } } }
作用有点像
StringTokener
那样的
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.charAt(p))
return last;
}
last = current;
current = wb.next();
}
return BreakIterator.DONE;
}
不过这个好像就是DOC里面的内容啊·········
那里面的程序也看过
不过是在没有什么帮助
BreakIterator 是一个功能很强的类
你可以通过它提供的静态方法 生成四种实例getCharacterInstance() 用于字符 的截断
getLineInstance() 用于行 的截断
getSentenceInstance() 用于句子 的截断
getWordInstance() 用于字 的截断
也就是说从一段 String 中获得 字符/行/句子/字 的Token获得实例后 用 setText() 方法 设置用于处理的字符串
然后用 next() 方法获得 下一个截断点的 位置
用 previous() 方法获得 上一个截断点的 位置
能给我个不是doc里面的例子吗?
谢谢
用 getWordInstance 举例 其他的是一样的import java.text.*;public class BreakIteratorTest{
public static void main(String[] args) throws Exception{
String str = "The BreakIterator class implements methods for finding the location of boundaries in text.";
BreakIterator bi = BreakIterator.getWordInstance();
bi.setText(str); int next = bi.next();
System.out.println(next);
System.out.println(str.charAt(next-1));
System.out.println(str.substring(0,next)); // The
System.out.println(bi.isBoundary(2)); //false
System.out.println(bi.isBoundary(3)); //true
System.out.println("================================="); // Print all the elements
int start = bi.first();
for (int end = bi.next(); end != BreakIterator.DONE; start = end, end = bi.next()) {
System.out.println(str.substring(start,end));
}
}
}