Java 将子字符串替换为字符串中的空白
我有一根绳子Java 将子字符串替换为字符串中的空白,java,json,string,Java,Json,String,我有一根绳子 String me = "I am ugly and not handsome." 我想去 I am ugly, not handsome. 所以我需要用“,”替换“和”。我想我可以用它来做 String.replace(" and ", ", ") 但是,它会忽略空格,并查找和的所有实例。因此,这种情况发生了: I am ugly, not h,dsome 我在一个字符串解析程序中使用这个。它在数千行上迭代,所以我希望它速度高效。我不知道我所做的是“速度效率”还是“速度效
String me = "I am ugly and not handsome."
我想去
I am ugly, not handsome.
所以我需要用“,”替换“和”。我想我可以用它来做
String.replace(" 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"}]
},
]
目前,我正在通过逐行迭代文件并将该行解析为一个实体来解析它。但我相信,当格式不符合所需的解析器格式时,这将给我带来未来的问题。您可以尝试下面的代码吗
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);
}
}
带有
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);
}
不知何故,在编辑器中复制时,和
的前导和尾随空格可能丢失了
它通常比正则表达式快
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();
最快的可能是: 试试这个,很简单 输入:我很丑,不帅
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, ",");
输出:我很丑,不帅。尝试使用正则表达式?只需替换“我很丑”和“我很丑”,就无法重现这种行为。我得到了“我很丑,不帅。”结果正如你所希望的。你能添加更多的代码细节吗,“并且”不会省略空格我的默认使用
String.replace(“\\s and\\s”,”,”)
我想到了这一点,但由于时间复杂性,不敢实现它。但我一定要试试这个。嗯,也许是这样。但是关于速度,它真的比正则表达式快吗?因为在我的测试中,迭代了数千个文件,超出了预期的执行时间。最后一个应该是最快的。第一个正则表达式也是正确的。公认的答案可能确实是总体上最快的答案。
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, ",");