I have a string
我有一个字符串
String me = "I am ugly and not handsome."
I want to make it
我想成功
I am ugly, not handsome.
So I need to replace " and " with ", ". Supposedly I can do it with
所以我需要用“,”替换“和”。据说我可以做到
String.replace(" and ", ", ")
However, it omits the whitespaces and looks for all instances of and
. So this happens instead:
但是,它省略了空格并查找和的所有实例。所以这发生了:
I am ugly, not h,dsome
I'm using this in a string parsing program. It's iterating over thousands of lines, so I want it to be speed efficient. I don't know if what I'm doing is "speed efficient" or what not if you have any other opinions I would appreciate it. Sample file:
我在字符串解析程序中使用它。它迭代了数千行,所以我希望它具有速度效率。我不知道我正在做的是“速度有效”,或者如果你有任何其他意见,我会很感激。示例文件:
[and & , , , --- 1] (datetime)
[and & , , , --- 2] (datetime) - You are kind
[and & , , , --- 3] (datetime) - word1, word2 & wor&d3
[and & , , , --- 4] (Datetime) - word1, word2andword3, and word3
For the sake of clarity on why I'm trying to achieve this and just in case someone has a better solution: The project I'm working on needs to parse this into a Json as so:
为了清楚我为什么要实现这个目的,以防万一有人有更好的解决方案:我正在研究的项目需要将其解析为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"}]
},
]
Currently, I'm parsing it by iterating the file line by line and parsing the line into an entity. But I believe this will give me future problems when the format does not follow the desired parser format.
目前,我正在通过逐行迭代文件并将该行解析为实体来解析它。但是我相信当格式不符合所需的解析器格式时,这将给我带来未来的问题。
3 个解决方案
#1
1
Your code with String.replace
works fine and is faster than regex replaceAll.
使用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);
}
Somehow while copying in the editor, the leading and trailing spaces for and
got lost perhaps.
不知何故,在编辑器中复制时,前导和尾随空格也许会丢失。
It normally would be faster than regex
它通常比正则表达式更快
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();
However the regex can deal better with whitespace, and actually replace(String, String)
is implemented with regex too. So having the pattern compiled only once (a time intensive operation for complex patterns), might actually make the regex faster. The optimum would be using a non-regex Pattern:
但是正则表达式可以更好地处理空格,实际上替换(String,String)也是用regex实现的。因此,只编译一次模式(复杂模式的时间密集型操作),实际上可能使正则表达式更快。最佳方案是使用非正则表达式:
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();
The fastest might be:
最快的可能是:
private static final Pattern AND_PATTERN = Pattern.compile(" and ", Pattern.LITERAL);
...
String changes = PATTERN.matcher(me).replaceAll(", ");
#2
1
Can you please try below code
你可以试试下面的代码
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
1
try this, very simple
试试这个,非常简单
Input : I am ugly and not handsome.
输入:我很丑,不帅。
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, ",");
Output : I am ugly, not handsome.
输出:我很丑,不帅。
#1
1
Your code with String.replace
works fine and is faster than regex replaceAll.
使用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);
}
Somehow while copying in the editor, the leading and trailing spaces for and
got lost perhaps.
不知何故,在编辑器中复制时,前导和尾随空格也许会丢失。
It normally would be faster than regex
它通常比正则表达式更快
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();
However the regex can deal better with whitespace, and actually replace(String, String)
is implemented with regex too. So having the pattern compiled only once (a time intensive operation for complex patterns), might actually make the regex faster. The optimum would be using a non-regex Pattern:
但是正则表达式可以更好地处理空格,实际上替换(String,String)也是用regex实现的。因此,只编译一次模式(复杂模式的时间密集型操作),实际上可能使正则表达式更快。最佳方案是使用非正则表达式:
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();
The fastest might be:
最快的可能是:
private static final Pattern AND_PATTERN = Pattern.compile(" and ", Pattern.LITERAL);
...
String changes = PATTERN.matcher(me).replaceAll(", ");
#2
1
Can you please try below code
你可以试试下面的代码
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
1
try this, very simple
试试这个,非常简单
Input : I am ugly and not handsome.
输入:我很丑,不帅。
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, ",");
Output : I am ugly, not handsome.
输出:我很丑,不帅。