【问题标题】:Replace substring with whitespace in string用字符串中的空格替换子字符串
【发布时间】:2018-01-30 13:46:01
【问题描述】:

我有一个字符串

String me = "I am ugly and not handsome."

我想成功

I am ugly, not handsome.

所以我需要将 " 和 " 替换为 "、"。据说我可以用

String.replace(" and ", ", ")

但是,它会忽略空格并查找 and 的所有实例。所以会发生这种情况:

I am ugly, not h,dsome

我在字符串解析程序中使用它。它迭代了数千行,所以我希望它能够提高速度。我不知道我所做的是否是“速度高效”,或者如果您有任何其他意见我会很感激。 示例文件:

[and & , , , --- 1] (datetime)
[and & , , , --- 2] (datetime) - You are kind
[and & , , , --- 3] (datetime) - word1, word2 & wor&d3
[and & , , , --- 4] (Datetime) - word1, word2andword3, and word3

为了清楚说明我为什么要实现这一目标,以防万一有人有更好的解决方案: 我正在从事的项目需要将其解析为 Json:

[
{
"message":"and & , , , --- 1",
"timestamp":"datetime",
"content":[]
},
{
"message":"and & , , , --- 2",
"timestamp":"datetime",
"content":[{"text":"You are kind"}]
},
{
"message":"and & , , , --- 3",
"timestamp":"datetime",
"content":[{"text":"word1"},{"text":"word2"},{"text":"wor&d3"}]
},
{
"message":"and & , , , --- 4",
"timestamp":"datetime",
"content":[{"text":"word1"},{"text":"word2andword3"},{"text":"word3"}]
},
]

目前,我通过逐行迭代文件并将该行解析为实体来解析它。但是我相信当格式不遵循所需的解析器格式时,这会给我带来未来的问题。

【问题讨论】:

  • 试过使用正则表达式吗?
  • 只需替换“我很丑”和“我很丑”,
  • 无法重现此行为。我得到“我很丑,不帅”。结果如你所愿。
  • 你能添加更多的代码细节,“和”,不会省略空格我的默认
  • 使用String.replace(" \\s and \\s ", ", ")

标签: java json string


【解决方案1】:

带有String.replace 的代码运行良好,并且比正则表达式 replaceAll 更快。

@Test
public void testMirror() {
    String me = "I am ugly and not handsome.";
    String actual = me.replace(" and ", ", ");
    String expected = "I am ugly, not handsome.";
    Assert.assertEquals("hmm", expected, actual);
}

在编辑器中复制时,and 的前导和尾随空格可能会丢失。


它通常会比正则表达式更快

private static final Pattern AND_PATTERN = Pattern.compile("\\s+\\band\\b");
...
    Matcher matcher = PATTERN .matcher(me);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(sb, ",");
    }
    matcher.appendTail(sb);
    String changes = sb.toString();

然而,正则表达式可以更好地处理空白,实际上replace(String, String) 也是用正则表达式实现的。所以只编译一次模式 (复杂模式的时间密集型操作)实际上可能使正则表达式更快。最佳方案是使用非正则表达式模式:

private static final Pattern AND_PATTERN = Pattern.compile(" and ", Pattern.LITERAL);
...
    Matcher matcher = PATTERN .matcher(me);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(sb, ", ");
    }
    matcher.appendTail(sb);
    String changes = sb.toString();

最快的可能是:

private static final Pattern AND_PATTERN = Pattern.compile(" and ", Pattern.LITERAL);
...
    String changes = PATTERN.matcher(me).replaceAll(", ");

【讨论】:

  • 嗯也许是这样。但关于速度,它真的比正则表达式快吗?因为在我的测试中,迭代了数千个文件,它超出了预期的执行时间。
  • 我检查了有关 String.replace 的内容,并添加了两个正则表达式旧式解决方案。最后一个应该是最快的。第一个正则表达式也是正确的。接受的答案可能确实是总体上最快的答案。
【解决方案2】:

你能试试下面的代码吗

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringSpace {
    public static void main(String[] args) {
        String me = "I am ugly and not handsome.";
        String changes = null ;

        Pattern whitespace = Pattern.compile("\\s\\band\\b");
        Matcher matcher = whitespace.matcher(me);
        while (matcher.find()){
            changes = matcher.replaceAll(",");
        }
        System.out.println(changes);
    }
}

【讨论】:

  • 我想到了这一点,但由于时间复杂性而不敢实现它。但我一定会尝试一下。
【解决方案3】:

试试这个,很简单

输入:我丑不帅。

String str = "I am ugly and not handsome.";
int i = 0;
i = str.IndexOf(" and");
str = str.Remove(i, " and".Length);
str = str.Insert(i, ",");

输出:我丑,不帅。

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 2018-09-19
    • 2012-08-09
    • 2013-05-09
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    相关资源
    最近更新 更多