Difference Between replace() and replaceall() in Java
In this article, you will be acknowledged about the replace() and replaceall() methods in java. Also, most importantly you will be acknowledged the differences between them along with the example programs.
replace()
The replace() method of a Java String class returns a string that replaces every old char as well as CharSequence with a new char for CharSequence.
A new replace() function that has been available since JDK 1.5 enables us just to replace a series of char values.
If the replacement or target is equivalent to null, a NullPointerException will occur.
This method can be used for many purposes like replacing only the characters in a string, or replacing the sequence of characters in a string. Let us go through few example programs to understand.
Let’s us now understand the below program flow in which the replacement of single characters takes place.
File name: Replace1.java
public class Replace1{
public static void main(String args[]){
String s1="my name is Aiden Smith";
String replaceString=s1.replace('m','o');//All m’s will be replaced with o.
System.out.println(replaceString);
}}
Output
oy naoe is Aiden Soith
Now, let us understand the below example program that depicts the replacement of sequence of characters.
File name: Replace2.java
public class Replace2{
public class Replace2{
public static void main(String args[]){
String s1="my name is Aiden Smith my wife’s name is Kenzie Madison";
String replaceString=s1.replace("my","our");//my would be replaced with our
System.out.println(replaceString);
}}
Output
our name is Aiden Smith our wife’s name is Kenzie Madison
When the replacements or target is null, the replace() method raises the NullPointerException. The below program depicts this.
File name: Replacenull.java
public class Replacenull
{
public static void main(String argvs[])
{
String str = "My friend is a good soccer player. He is a champion.";
int size = str.length();
System.out.println(str);
String target = null;
str = str.replace(target, "JavaTpoint ");
System.out.println(str);
}
}
Output
My friend is a good soccer player. He is a champion.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.CharSequence.toString()" because "target" is null
at java.base/java.lang.String.replace(String.java:2954)
at Replacenull.main(Replacenull.java:11)
replaceall()
The replaceAll() method of Java String class returns a string that replaces every sequence of characters that match the pattern and replacement string.
If the regular expression's syntax is incorrect, a PatternSyntaxException will occur.
Let us understand the replaceall() method by few simple example programs
Replaceall.java
public class Replaceall1{
public static void main(String args[]){
String s1="my wife name is kenzie madisson";
String replaceString=s1.replaceAll("k","l");// All k’s are replaced by l.
System.out.println(replaceString);
}}
Output
my wife name is lenzie madisson
Now, let us go through how the sequence of characters are replaced
File name: Replaceall2.java
public class Replaceall2{
public static void main(String args[]){
String s1="My name is Aiden. My wife name is Kenzie Madisson.";
String replaceString=s1.replaceAll("Kenzie","Benzie");//replaces all occurrences of "is" to "was"
System.out.println(replaceString);
}}
Output
My name is Aiden. My wife name is Benzie Madisson.
When there is an incorrect regular expression, the replaceAll() function throws the PatternSyntaxException. Check out at the example below.
File name: Replaceallnull.java
public class Replaceallnull
{
public static void main(String argvs[])
{
// input string
String str = "Every Sunday is the end of the week.";
System.out.println(str);
String regex = "\\";
str = str.replaceAll(regex, "Sunday ");
System.out.println(str);
}
}
Output
Every Sunday is the end of the week.
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
at java.base/java.util.regex.Pattern.error(Pattern.java:2038)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1799)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1440)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1079)
at java.base/java.lang.String.replaceAll(String.java:2938)
at Replaceallnull.main(Replaceallnull.java:13)
The functional differences are
- replace() replaces every instance of the old char with the new char, whereas replaceAll() replaces every instance of the old string with current string. This is the difference between the two methods. Basically, replace() replaces characters and replaces All() can replace individual characters in strings.
- The String. replaceFirst() function and replaceAll() are equivalent. The only distinction amongst them is that for all instances where the sub-string appears in the string, the provided string is used in lieu of it.
- A string can be replaced with a specific substring by employing the replace() and replaceAll() methods. Both approaches have the same names, but they operate in different ways.
- replace() replaces every instance of the old char with the new char, whereas replaceAll() replaces every instance of the old string with new string. This is the difference between the two methods.