Apache StringUtils 与 Java 实现的 replace()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7012000/
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
Apache StringUtils vs Java implementation of replace()
提问by Oh Chin Boon
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?
Java 1.4.2 的 replace 实现和 Apache 2.3 的实现有什么区别?是否有一种性能增益?
回答by Joachim Sauer
The String.replace()
method you linked to takes two char
values, so it only ever replaces on character with another (possibly multiple times, 'though).
String.replace()
您链接到的方法采用两个char
值,因此它只会将字符替换为另一个(可能多次,'虽然)。
The StringUtils.replace()
method on the other hand takes String
values as the search string and replacement, so it can replace longer substrings.
StringUtils.replace()
另一方面,该方法将String
值作为搜索字符串和替换,因此它可以替换更长的子字符串。
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 中的类似方法是replaceAll()
. replaceAll()
可能比StringUtils
方法慢,因为它支持正则表达式,因此引入了首先编译搜索字符串并运行正则表达式搜索的开销。
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.
请注意,Java 5 引入了String.replace(CharSequence, CharSequence)
which 做同样的事情StringUtils.replace(String,String)
(除了如果它的NullPointerException
任何参数是,它会抛出 a null
)。请注意,这CharSequence
是由 实现的接口String
,因此您可以String
在此处使用普通的旧对象。
回答by sreenath V
public class Compare {
public static void main(String[] args) {
StringUtils.isAlphanumeric(""); // Overhead of static class initialization for StringUtils
String key = "0 abcdefghijklmno" + Character.toString('\n') + Character.toString('\r');
String key1 = replace1(key);
String key2 = replace2(key);
}
private static String replace1(String key) {
long start = System.nanoTime();
key = StringUtils.replaceChars(key, ' ', '_');
key = StringUtils.replaceChars(key, '\n', '_');
key = StringUtils.replaceChars(key, '\r', '_');
long end = System.nanoTime() - start;
System.out.println("Time taken : " + end);
return key;
}
public static String replace2(String word) {
long start = System.nanoTime();
char[] charArr = word.toCharArray();
int length = charArr.length;
for (int i = 0; i < length; i++) {
if (charArr[i] == ' ' || charArr[i] == '\n' || charArr[i] == '\r') {
charArr[i] = '_';
}
}
String temp = new String(charArr);
long end = System.nanoTime() - start;
System.out.println("Time taken : " + end);
return temp;
}
}
Time taken : 6400
Time taken : 5888
耗时:6400
耗时 : 5888
Times are almost the same!
时间几乎一样!
I've edited the code to drop out overheads of replace2
which were not because of JDK implementation.
我已经编辑了代码以删除replace2
不是因为 JDK 实现的开销。
回答by adarshr
1.4.2 replaces operates only with char
arguments whereas the Apache 2.3 one takes in strings.
1.4.2 替换仅使用char
参数进行操作,而 Apache 2.3 则接受字符串。
回答by Bozho
String.replace(char, char)
can't replace whole strings- you can have
null
values withStringUtils.replace(..)
.
String.replace(char, char)
不能替换整个字符串- 您可以
null
使用StringUtils.replace(..)
.
String.replace(CharSequence s1, CharSequence s2)
will do the same thing if the first string is not-null. Otherwise it will throw a NullPointerException
String.replace(CharSequence s1, CharSequence s2)
如果第一个字符串不为空,也会做同样的事情。否则它会抛出一个NullPointerException
回答by Maitrik B Panchal
To replace a string character with another string using StringUtil.Replace
, I tried following and it's working fine for me to replace multiple string values from a single string.
要将一个字符串字符替换为另一个字符串StringUtil.Replace
,我尝试了以下操作,从单个字符串替换多个字符串值对我来说效果很好。
String info = "[$FIRSTNAME$]_[$LASTNAME$]_[$EMAIL$]_[$ADDRESS$]";
String replacedString = StringUtil.replace(info, new String[] { "[$FIRSTNAME$]","[$LASTNAME$]","[$EMAIL$]","[$ADDRESS$]" }, new String[] { "XYZ", "ABC" ,"[email protected]" , "ABCD"});
This will replace the String value of info with newly provided value...
这将用新提供的值替换信息的字符串值...
回答by Brian
Apache's is quite a bit faster, if I recall correctly. Recommended.
如果我没记错的话,Apache 的速度要快一些。受到推崇的。