Apache StringUtils与Java实现的replace() [英] Apache StringUtils vs Java implementation of replace()
问题描述
Java 1.4.2的replace实现与Apache 2.3的实现有什么区别?是否有性能上升?
What would be the difference between Java 1.4.2's implementation of replace, and Apache 2.3's implementation? Is there a performance gain one over another?
推荐答案
字符串。你链接的replace()
方法需要两个 char
值,因此它只会将字符替换为另一个字符(可能是多次,但是)。
The String.replace()
method you linked to takes two char
values, so it only ever replaces on character with another (possibly multiple times, 'though).
另一方面, StringUtils.replace()
方法需要 String
值作为搜索字符串和替换,因此它可以替换更长的子字符串。
The StringUtils.replace()
method on the other hand takes String
values as the search string and replacement, so it can replace longer substrings.
Java中的可比方法是 replaceAll()
。 replaceAll()
可能比 StringUtils
方法慢,因为它支持正则表达式,因此引入了开销首先编译搜索字符串并运行正则表达式搜索。
The comparable method in Java would be replaceAll()
. replaceAll()
is likely to be slower than the StringUtils
method, because it supports regular expressions and thus introduces the overhead of compiling the search string first and running a regex search.
请注意,Java 5引入了 String.replace(CharSequence,CharSequence)
与 StringUtils.replace(String,String)
完全相同(除了它抛出 NullPointerException
如果它的任何参数是 null
)。请注意 CharSequence
是由 String
实现的接口,因此您可以使用普通的旧 String
此处的对象。
Note that Java 5 introduced String.replace(CharSequence, CharSequence)
which does the same thing as StringUtils.replace(String,String)
(except that it throws a NullPointerException
if any of its arguments are null
). Note that CharSequence
is an interface implemented by String
, so you can use plain old String
objects here.
这篇关于Apache StringUtils与Java实现的replace()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!