如何删除字符串中的额外空格和新行?

时间:2015-06-28 14:10:45

标签: java arrays string

我有一个字符串变量s,就像一个段落的组合。 例如,

Passages provides funeral and burial products.

Our products are meant to align with your values and bring you comfort.

Our products allow you to offer personalization , flexibility and innovative choices, helping you provide services to a wider range of customers.

我必须创建这种形式的字符串变量:

Passages provides funeral and burial products. Our products are meant to align with your values and bring you comfort. Our products allow you to offer personalization, flexibility and innovative choices, helping you provide services to a wider range of customers.

另外,要删除单词之间的额外空格(或在单词的'。'和第一行之间),转换为单个空格和',','。'之前的任意数量的空格。要么 ';'将被删除。

我是java的新手。谁能告诉我怎么办呢?

6 个答案:

答案 0 :(得分:2)

试试这个:( @ Criti' s)

    String s = "Passages provides funeral and burial products.\n"
            + "Our products are meant to align with your values and bring you comfort.\n"
            + "Our products allow you to offer personalization , flexibility and innovative choices, helping you provide services to a wider range of customers.";

    s = s.replaceAll("\\s*\\.\\s*\n\\s*", ". ");
    s = s.replaceAll("\\s*,\\s*", ", ");
    s = s.replaceAll("\\s*;\\s*", "; ");
    System.out.println(s);

输出:

Passages provides funeral and burial products. Our products are meant to align with your values and bring you comfort. Our products allow you to offer personalization, flexibility and innovative choices, helping you provide services to a wider range of customers.

答案 1 :(得分:1)

string.replaceAll("\n", "").replaceAll("\\s+", " ")

答案 2 :(得分:1)

正则表达式的唯一问题是它们可能非常慢。如果您愿意使用外部库,请尝试使用Google Guava库及其CharMatcher

CharMatcher.WHITESPACE.collapseFrom(" Hello There \ n我的名字是Fred",'')

这会将空格转换为单个空格,并将多个空格序列折叠为单个序列。

答案 3 :(得分:0)

一种方法是逐字符解析String变量。 例如

public IEnumerator<T> GetEnumerator()
{
    var count = Count;
    for (var i = 0; i < count; ++i)
        yield return this[i];
}

另一种方法是使用常规表达式:

StringBuilder sb = new StringBuilder();
String toBeParse = "...";
for (int i = 0; i < toBeParse.length(); i++) {
    if (toBeParse.charAt(i) == condition) {
        sb.append(toBeParse.charAt(i));
    }
}
String result = sb.toString();

答案 4 :(得分:0)

试试这个

str = str.replaceAll("\\.\\s+(\\w)", ". $1");

答案 5 :(得分:0)

我是Apache Commons Lang库的忠实粉丝 - StringUtils类(具有零安全功能)多年来为我节省了无数个小时。毫不奇怪,StringUtils有一个功能可以满足您的需求:StringUtils.normalizeSpace(String str)

来自API:

  

该函数返回带有标准化空格的参数字符串   使用trim(String)删除前导和尾随空格然后   用单个空格替换空格字符序列。