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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Times are almost the same!
I've edited the code to drop out overheads of
replace2
which were not because of JDK implementation.1.4.2 replaces operates only with
char
arguments whereas the Apache 2.3 one takes in strings.The
String.replace()
method you linked to takes twochar
values, so it only ever replaces on character with another (possibly multiple times, 'though).The
StringUtils.replace()
method on the other hand takesString
values as the search string and replacement, so it can replace longer substrings.The comparable method in Java would be
replaceAll()
.replaceAll()
is likely to be slower than theStringUtils
method, because it supports regular expressions and thus introduces the overhead of compiling the search string first and running a regex search.Note that Java 5 introduced
String.replace(CharSequence, CharSequence)
which does the same thing asStringUtils.replace(String,String)
(except that it throws aNullPointerException
if any of its arguments arenull
). Note thatCharSequence
is an interface implemented byString
, so you can use plain oldString
objects here.Apache's is quite a bit faster, if I recall correctly. Recommended.
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.This will replace the String value of info with newly provided value...
String.replace(char, char)
can't replace whole stringsnull
values withStringUtils.replace(..)
.String.replace(CharSequence s1, CharSequence s2)
will do the same thing if the first string is not-null. Otherwise it will throw aNullPointerException