所以我写了下面的代码:
String text = "This is a string. I want to break it into sentences";
String[] sentences = text.split("\\.");
for (int i = 0; i < sentences.length; i++)
System.out.println(sentences[i]);
此代码的输出为:
This is a string
I want to break it into sentences
如何更改此代码以便
例如,如果我们有以下字符串
String text = "This is a string! Is this a string? I want to break it into sentences";
然后输出应该是:
This is a string
Is this a string
I want to break it into sentences
答案 0 :(得分:1)
您可以使用字符类来分割点(.
),?
或!
字符中的任何一个。要删除句子开头(可能还有结尾)的空格,您可以简单地修剪结果字符串:
String[] sentences = text.split("[.!?]");
for (int i = 0; i < sentences.length; i++) {
System.out.println(sentences[i].trim());
}
答案 1 :(得分:1)
将分隔符放在字符类中,并在char类旁边添加\\s*
,这样它也会消耗以下零个或多个空格。
String[] sentences = text.split("[?!.]\\s*");
示例:强>
String text = "This is a string! Is this a string? I want to break it into sentences";
String[] parts = text.split("[?!.]\\s*");
for(String i: parts)
{
System.out.println(i);
}
<强>输出:强>
This is a string
Is this a string
I want to break it into sentences