删除句子和段落之间的各种额外空格

时间:2014-04-29 06:48:11

标签: java regex string

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

例如:

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.

如果我在控制台中复制上面的字符串,那么只需要第一个句子,然后对其进行评估。我希望将整个段落作为字符串。这是可能的,我在这里尝试了很多,但它只删除句子中的空格。因此,如果我们删除单词之间的空格,则没有任何意义。我想删除句子和段落之间的空格    。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:6)

使用正则表达式:

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

答案 1 :(得分:1)

尝试这样的事情:

    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 +","")

答案 2 :(得分:0)

我希望以下代码段可以帮助您。

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.