用字符串中的空格替换子字符串

时间:2018-01-30 13:46:01

标签: java json string

我有一个字符串

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

我想成功

I am ugly, not handsome.

所以我需要更换" " "," 。据说我可以用

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

但是,它会省略空格并查找 and 的所有实例。所以这恰好发生了:

I am ugly, not h,dsome

我在字符串解析程序中使用它。它迭代了数千行,所以我希望它具有速度效率。我不知道我做的是什么,#34;速度效率高,#34;或者如果你有任何其他意见,我会很感激。 样本文件:

[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"}]
},
]

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

3 个答案:

答案 0 :(得分:1)

请试试下面的代码

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

答案 1 :(得分:1)

String.replace的代码运行正常,速度比regex 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(", ");

答案 2 :(得分:1)

试试这个,非常简单

输入:我很丑,不帅。

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

输出:我很丑,不帅。

相关问题