In this post, we will see difference between String’s replace()
and replaceAll()
methods in java.
Table of Contents
String’s replace() takes either two chars or two CharSequences as arguments and it will replace all occurrences of char or String but replaceAll() method takes regex String as argument and replaces each substring of that matches given regex with replacement string. If you use wrong method, then it can introduce bug in the code. That’s why it is good to know difference between replace() and replaceAll() methods in java.
Let’s understand replace()
and replaceAll()
with the help of examples.
replace()
You should use replace if you want to replace one char with another or one String with another.
replace char with another char using replace()
Here is an example to replace one char with another using replace()
method. Here we will use replace()
method to replace char ‘p’ with char ‘b’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; public class ReplaceStringMain { public static void main(String args[]) { String str = "Java C C++ Java"; char oldString = "Java"; char newString = "Python"; String resultStr = str.replace(oldString, newString); System.out.println(resultStr); } } |
Output:
As you can see, char ‘p’ got replaced with ‘b’ in the output.
replace String with another String using replace()
Here is an example to replace one String with another String using replace()
method. Here we will use replace()
method to replace String "java"
with "Python"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; public class ReplaceStringMain { public static void main(String args[]) { String str = "Java C C++ Java"; String oldString = "Java"; String newString = "Python"; String resultStr = str.replace(oldString, newString); System.out.println(resultStr); } } |
Output:
As you can see, String "Java" got replace with "Python" in the output.
replaceAll()
You should use String’s replaceAll()
method if you want to use regular expressions.
replace any number with char
Here is example to replace any number with char using replaceAll() method. Here we will use regular expression
to replace any number to char ‘a’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; public class ReplaceAllStringMain { public static void main(String args[]) { String str = "012345___"; String regex = "\\d"; String replacement = "a"; String resultStr = str.replaceAll(regex, replacement); System.out.println(resultStr); } } |
Output:
As you can see, all the numbers got replaced with char 'a'
in the output.
remove all whitespace in the String
You can use replaceAll()
method to remove all whitespaces in the String by passing required regular expression to it.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; public class ReplaceAllStringMain { public static void main(String args[]) { String str = " Java \r is\n\n Programming \t\t language "; String regex = "\\s"; String replacement = ""; String resultStr = str.replaceAll(regex, replacement); System.out.println(resultStr); } } |
Output:
As you can see, String’s whicespaces got replaced with "" in the output.
That’s all about difference between replace and replaceAll methods in java.