【问题标题】:How to remove extra spaces and new lines in a string?如何删除字符串中多余的空格和换行符?
【发布时间】:2015-06-28 14:10:45
【问题描述】:

我有一个字符串变量 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新手。谁能告诉我怎么做?

【问题讨论】:

  • 看看Java中的正则表达式。
  • 您的规则似乎不一致。如果您使用 exact 格式规则编辑您的问题,您可能会得到一个有用的答案。
  • 您可以遍历字符串并使用条件复制所有不是\n或任何其他字符的字符。
  • 我已经实现了所有规则,除了如何删除单词和',','之间的所有空格。或';'(单词首先出现)

标签: java arrays string


【解决方案1】:

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

来自 API:

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

【讨论】:

    【解决方案2】:

    试试这个:(@Criti 的方式)

        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.
    

    【讨论】:

    • 单词和'.'之间的空格或者 ';'没有在您的解决方案中被删除。例如:'这很酷。那很热。不会变成“这很酷”。太热了。'
    • 已编辑;你能再试一次吗?
    【解决方案3】:

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

    CharMatcher.WHITESPACE.collapseFrom("你好\n我叫 Fred ", ' '))

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        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();
        

        另一种方法是使用正则表达式:

        toBeParse.replaceAll(yourRegexString, replacement);
        

        【讨论】:

          【解决方案6】:

          试试这个

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-06-03
            • 2011-08-05
            • 1970-01-01
            • 1970-01-01
            • 2013-10-09
            相关资源
            最近更新 更多