Difference between String replace() and replaceAll() in Java
It is a common misunderstanding that replace()
will replace that particular character from the string and replaceAll()
will replace all the occurrences of that particular character.
Let's understand the correct usage of replace()
and replaceAll()
.
java.lang.String
String
class as well as the methods replace()
and replaceAll()
are part of java from the initial version.
It represents the character strings. All the string literals in java are implemented as instances of String
class.
The String
class includes a lot of methods to travel through its characters. Let's see replace()
and replaceAll()
in detail.
replace()
public String replace(char oldChar, char newChar)
This method replaces
If the character oldChar does not occur in the string, then a reference to this String object is returned. Otherwise, a String object is returned that represents the same String object, except that every occurrence of oldChar is replaced by newChar.
Since String is immutable, once a string literal is created, its value cannot be changed. So to see the updated string after calling replace(), it has to be assigned back.
Consider the below example.
String str = "John is a brilliant student. John is the class topper too.";
str.replace("John", "Ryan");
System.out.println(str);
str = str.replace("John", "Ryan");
System.out.println(str);
--Output--
John is a brilliant student. John is the class topper too.
Ryan is a brilliant student. Ryan is the class topper too.
You can see that the string "John" has been replaced by "Ryan" in all the placces.
replaceAll()
Let's try the above example with replaceAll()
insted of replace
.
String str = "John is a brilliant student. John is the class topper too.";
str.replaceAll("John", "Ryan");
System.out.println(str);
str = str.replaceAll("John", "Ryan");
System.out.println(str);
--Output--
John is a brilliant student. John is the class topper too.
Ryan is a brilliant student. Ryan is the class topper too.
If you see both the outputs, both are exactly the same.
Then,
Let's compare the signatures of both the methods.
public String replace(char oldChar, char newChar)
public String replaceAll(String regex, String replacement)
Ok, now its makes sense. If you have a lot of places you want replacement, you don't have the exact string, but have some patterns (regular expressions will help to create those patterns) available with those strings, you can do it by using the replaceAll()
method.
It doesn't matter, whether you are passing a regular expression or a simple character string, the underlying implementation of replaceAll()
calls the Pattern.compile()
method each time when you invoke replaceAll()
. So if you think about code performance, these methods should be used wisefully.
Let's see an example of replaceAll() working with regular expression.
System.out.println(
System.out.println(
--Output--
John is a brilliant student. John is the class topper too.
Ryan is a brilliant student. Ryan is the class topper too.
You can see that replace()
is not able to capture the string "John". But replaceAll()
is working as expected.
replaceAll()
usage, if the first parameter is not a regular expression.If the regular expression syntax is invalid,
replaceAll()
will throw PatternSyntaxException
Conclusion
- » We have learnt the usage of both
replace()
andreplaceAll()
. - » Performance issues when
replaceAll()
is used blindly.