I am trying to know the pros and cons of using apache.commons.StringUtils.replacePattern()
instead of the java.lang.String.replaceAll()
function.
I heard that the replacePattern()
function improves the performance but I could not find that stated in any of the javaDocs or in any of the popular java forums. Please let me know if there are any other advantages of using the replacePattern()
method.
Here's the source of String.replaceAll from OpenJDK 8:
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
Here's the source of StringUtils.replacePattern:
public static String replacePattern(final String source, final String regex, final String replacement) {
return Pattern.compile(regex, Pattern.DOTALL).matcher(source).replaceAll(replacement);
}
The only difference between these methods is that replacePattern passes the DOTALL option and replaceAll doesn't.