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?
What would be the difference between Java 1.4.2’s implementation of replace, and Apache 2.3’s
Share
The
String.replace()method you linked to takes twocharvalues, so it only ever replaces on character with another (possibly multiple times, ‘though).The
StringUtils.replace()method on the other hand takesStringvalues 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 theStringUtilsmethod, 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 aNullPointerExceptionif any of its arguments arenull). Note thatCharSequenceis an interface implemented byString, so you can use plain oldStringobjects here.