Difference Between String Replace() and Replaceall()

Difference between String replace() and replaceAll()

In java.lang.String, the replace method either takes a pair of char's or a pair of CharSequence's (of which String is a subclass, so it'll happily take a pair of String's). The replace method will replace all occurrences of a char or CharSequence. On the other hand, the first String arguments of replaceFirst and replaceAll are regular expressions (regex). Using the wrong function can lead to subtle bugs.

Difference in behaviour between String.replaceAll() and StringUtils.replace()

You don't need to escape the single quote in Strings (only in char values), and you need a double-double escape with the replacement value:

innerValue.replaceAll("'", "\\\\'")

This is due to replaceAll taking regular expressions as arguments (the second parameter must support regular expressions for back references).

You can also use the replace idiom, since you're not using regular expressions:

innerValue.replace("'", "\\'")

Note

The replace method actually uses replaceAll behind the scenes, but transforms values into literals by invoking Pattern.compile with the Pattern.LITERAL flag, and Matcher.quoteReplacement on the replacement.

Difference between str.replaceAll() and str = str.replaceAll()

str.replaceAll("abcd", "") doesn't modify the source String, since Strings are immutable. Instead, it creates a new String and returns it. That's the reason you have to assign the returned String back to str.

String replaceAll() vs. Matcher replaceAll() (Performance differences)

According to the documentation for String.replaceAll, it has the following to say about calling the method:

An invocation of this method of the
form str.replaceAll(regex, repl)
yields exactly the same result as the
expression

Pattern.compile(regex).matcher(str).replaceAll(repl)

Therefore, it can be expected the performance between invoking the String.replaceAll, and explicitly creating a Matcher and Pattern should be the same.

Edit

As has been pointed out in the comments, the performance difference being non-existent would be true for a single call to replaceAll from String or Matcher, however, if one needs to perform multiple calls to replaceAll, one would expect it to be beneficial to hold onto a compiled Pattern, so the relatively expensive regular expression pattern compilation does not have to be performed every time.

Java: String.replaceAll() vs matcher.replaceAll() in a loop

In OpenJDK, String.replaceAll is defined as follows:

    public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}

[code link]

So at least with that implementation, it won't give better performance than compiling the pattern only once and using Matcher.replaceAll.

It's possible that there are other JDK implementations where String.replaceAll is implemented differently, but I'd be very surprised if there were any where it performed better than Matcher.replaceAll.



[…] due to not having to initialize the matcher every time. (I understand the matcher exists already, so you are not recreating it.)

I think you have a misunderstanding here. You really do create a new Matcher instance on each loop iteration; but that is very cheap, and not something to be concerned about performance-wise.


Incidentally, you don't actually need a separate 'matcher' variable if you don't want one; you'll get exactly the same behavior and performance if you write:

   thisWord = regexPattern.matcher(Scanner.next()).replaceAll("");

Java String.replace/replaceAll not working

Strings are immutable in Java. Make sure you re-assign the return value to the same String variable:

str = str.replaceAll("\\[", "");

For the normal replace method, you don't need to escape the bracket:

str = str.replace("[", "");


Related Topics



Leave a reply



Submit