【问题标题】:Removing all sorts of extra spaces between a sentence and a paragraph删除句子和段落之间的各种多余空格
【发布时间】:2014-06-14 23:09:42
【问题描述】:

我想删除句子之间的各种多余空格,并将其作为一个字符串进行一些处理

例如:

The meaning of the phrase "ice cream" varies from one country to another. Phrases 
such as "frozen custard", "frozen yogurt", "sorbet", "gelato" and others are used 
to distinguish different varieties and styles.

In some countries, such as the United States, the phrase "ice cream" applies only
to a    specific variety, and most governments regulate the commercial use of
the   various terms according to the relative quantities of the main ingredients. 

Products that do not meet the criteria to be called ice cream are labelled
"frozen dairy dessert" instead. In other countries, such as Italy and 
Argentina, one word is used for all variants.Analogues made from dairy 
alternatives,  such as goat's or sheep's milk, or milk substitutes, are 
available for those who are lactose intolerant, allergic to dairy protein, 
or vegan.  The most popular flavours of ice cream in North America (based
 on consumer surveys) are vanilla and chocolate.

如果我在控制台中复制上面的字符串,那么它只需要第一句话然后评估它。我想把这整个段落作为一个字符串。这可能吗,我在这方面做了很多尝试,但它只删除了句子中的空格。因此,如果我们删除单词之间的空格是没有任何意义的。我想删除句子和段落之间的空格 .谁能帮帮我?

【问题讨论】:

标签: java regex string


【解决方案1】:

使用正则表达式:

myText.trim().replaceAll("\\s+", " ");

【讨论】:

  • 我本来打算建议[ ]{2,},但这对;)
  • @chopu- 读取所有文本并将其添加到字符串中。使用 String newPara = myText.trim().replaceAll("\\s+", " ");
  • @chopu - 考虑 Ankur 的回答。这是正确的。您可能只阅读一行,或者您可能替换字符串而不是追加。
  • @WhoAmI 问题是我正在通过控制台读取此输入。无论如何要将控制台输入写入文件。
  • @chopu - 然后将 while 循环与 hasNextLine() 一起使用并读取每一行。使用 StringBuilder 为段落创建一个字符串。然后根据“\\s+”进行拆分
【解决方案2】:

试试这样的:

    StringBuilder builder = new StringBuilder();
    BufferedReader reader = new BufferedReader(new FileReader("FILE-PATH"));
    String str = null;

    while ((str = reader.readLine()) != null) {
        builder.append(str.replaceAll("\\s+", ""));
    }

    // Complete paragraph without spaces.
    System.out.println(builder.toString());

注意:要删除段落之间的空格,您需要从字符串中替换 '\n' 换行符。

str.replaceAll("\n+", "")

【讨论】:

  • 感谢您的回复。但问题是我正在通过控制台阅读这一段
  • 那就更简单了。在这种情况下,您将收到整个段落作为单个字符串。在这种情况下,只需使用 'str.replaceAll("\\s+", "")'。
  • 不,我只想删除段落和句子之间的空格,而不是单词之间的空格
【解决方案3】:

希望下面的 sn-p 对您有所帮助。

public class RegexTest {

    public static void main(String[] args)
    {

        String text="this is para 1."
                + "\n\n"
                + "this is para 2."
                + "\n\n"
                + "This is para 3.";
        System.out.println("Text looks like :\n "+text);
        String text2=text.replaceAll("\\s", "");
        System.out.println("\nText2 looks like: \n"+text2);

    }
}

输出

Text looks like :
 this is para 1.

this is para 2.

This is para 3.

Text2 looks like: 
thisispara1.thisispara2.Thisispara3.

【讨论】: