【发布时间】:2011-05-21 09:23:49
【问题描述】:
我正在尝试将一个段落分成句子。到目前为止,这是我的代码:
import java.util.*;
public class StringSplit {
public static void main(String args[]) throws Exception{
String testString = "The outcome of the negotiations is vital, because the current tax levels signed into law by President George W. Bush expire on Dec. 31. Unless Congress acts, tax rates on virtually all Americans who pay income taxes will rise on Jan. 1. That could affect economic growth and even holiday sales.";
String[] sentences = testString.split("[\\.\\!\\?]");
for (int i=0;i<sentences.length;i++){
System.out.println(i);
System.out.println(sentences[i]);
}
}
}
发现两个问题:
- 只要遇到句点(“.”)符号,代码就会拆分,即使它实际上是一个句子。如何防止这种情况发生?
- 分割的每个句子都以空格开头。如何删除多余的空间?
【问题讨论】:
标签: java text-parsing