문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top