替换字符串中所有出现的子字符串 - 在 Java 中哪个更有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5407592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace all occurrences of substring in a string - which is more efficient in Java?
提问by Regex Rookie
I know of two ways of replacing alloccurrences of substring in a string.
我知道有两种方法可以替换字符串中所有出现的子字符串。
The regex way (assuming "substring-to-be-replaced" doesn't include regex special chars):
正则表达式方式(假设“要替换的子字符串”不包括正则表达式特殊字符):
String regex = "substring-to-be-replaced" + "+";
Pattern scriptPattern = Pattern.compile(regex);
Matcher matcher = scriptPattern.matcher(originalstring);
newstring = matcher.replaceAll("replacement-substring");
The String.replace() way:
String.replace() 方式:
newstring = originalstring.replace("substring-to-be-replaced", "replacement-substring");
Which of the two is more efficient (and why)?
两者中哪一个更有效(为什么)?
Are there more efficient ways than the above described two?
有没有比上述两种更有效的方法?
回答by Johan Sj?berg
String.replace()
uses regex underneath.
String.replace()
下面使用正则表达式。
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL)
.matcher(this ).replaceAll(
Matcher.quoteReplacement(replacement.toString()));
}
Are there more efficient ways than the above described two?
有没有比上述两种更有效的方法?
There are given that you operate on an implementation backed e.g., by an array, rather than the immutable String class (since string.replace
creates a newstring on each invocation). See for instance StringBuilder.replace().
假设您对由数组支持的实现进行操作,而不是不可变的 String 类(因为在每次调用时string.replace
都会创建一个新字符串)。参见例如StringBuilder.replace()。
Compiling a regex incurs quite alotof overhead which is clear when observing the Pattern source code. Luckily, Apache offers an alternative approach in StringUtils.replace()
which according to the source code(line #3732) is quite efficient.
编译正则表达式会产生大量开销,这在观察Pattern 源代码时很明显。幸运的是,Apache 提供了一种替代方法StringUtils.replace()
,根据源代码(第 3732 行)非常有效。
回答by Jeremy
Here's the source codefrom openjdk:
这是来自 openjdk的源代码:
public String replace(CharSequence target, CharSequence replacement) {
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}
回答by a CVn
Not having done any profiling or benchmarking, I'd say it's a fairly safe bet that if you don't need regex magic, then the overhead of the regular expression parser (which you'll get no matter what, in terms of memory as well as CPU usage) costs you a lot more than you can possibly gain on the other end.
没有做任何分析或基准测试,我会说这是一个相当安全的赌注,如果你不需要正则表达式魔术,那么正则表达式解析器的开销(无论如何你都会得到,就内存而言以及 CPU 使用率)花费您的成本远远超过您在另一端可能获得的收益。
回答by David Weiser
Instead of using string
s, which are immutable, use char
arrays or some other mutable type (such as StringBuffer
or StringBuilder
).
不要使用string
不可变的 s,而是使用char
数组或其他一些可变类型(例如StringBuffer
或 StringBuilder
)。
回答by user unknown
Shouldn't you compare replaceAll 2 times? However, for a single invocation it will hardly be measurable. And will you do millions of comparisions?
你不应该比较replaceAll 2次吗?但是,对于单个调用,它几乎是不可测量的。你会做数百万次比较吗?
Then I would expect 'compile' to be faster, but only, if you don't use a constant String without any pattern-rules.
然后我希望“编译”更快,但前提是您不使用没有任何模式规则的常量字符串。
Where is the problem in writing a micro benchmark? Or look up the source.
写微基准的问题在哪里?或者查一下源码。