在java中将字符串分解为句子(在指定组的符号出现之后)

时间:2015-03-21 18:41:12

标签: java regex eclipse string text

所以我写了下面的代码:

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

如何更改此代码以便

  1. 每个新句子不仅会在&#34;。&#34;之后创建,也会在&#34;!&#34;之后创建。或&#34;?&#34;。
  2. 句子开头不会有任何空格。
  3. 例如,如果我们有以下字符串

    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
    

2 个答案:

答案 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