Java replace vs replaceall: In our previous tutorial, we have seen & got an explanation on how to find the string length in java using length method. And today, we have come up with a new tutorial that helps to replace a string in java. Hence, we will learn completely about how to replace a string in java?
Also, you will find the difference between replace(), replaceAll(), and replaceFirst() methods. So, follow the available links and start understanding the concept of replacing strings in java quickly.
- How to Replace a String in Java?
- Java.lang.string.replace() method in Java
- Example of Java String replace() Method
- replaceAll() method in Java
- Example of Java String replaceAll() method
- replaceFirst() method in Java
- Example of Java replaceFirst() method
- replace() vs replaceAll() vs replaceFirst()
How to Replace a String in Java?
Replace vs replaceall java: There are three Java String methods that are mainly utilized for the replacement of a string with another string. They are as such:
- replace()
- replaceAll()
- replaceFirst()
All three can be helpful in illustrating how to replace a string in java. So, have a look at the first java string replace() method with an example and then continue with the other two methods too.
1. Java.lang.string.replace() method in Java
Java string replace vs replaceall: The replace() method of the String class is used to replace all the occurrences of an old character in this string with a new character.
Syntax:
The syntax of the string replace() method is given below:
public String replace(char oldChar, char newChar)
Here, oldChar specifies the characters to be replaced by the character specified by newChar.
Example of Java String replace() Method:
class StringReplace{ public static void main(String args[]){ String str1 = "Streng Tutoreal"; //replacing e with i. System.out.println(str1.replace('e', 'i')); } }
Output:
String Tutorial
Do Check:
2. replaceAll() method in Java
Replace vs replaceall: This method returns a String that replaces each substring with this string that matches the given regular expression with the given replacement.
Syntax:
The syntax of the replaceAll() method is given below:
public String replaceAll(String regex, String replacement)
Example of Java String replaceAll() method:
class StringReplace{ public static void main(String args[]){ String str1 = "String Tutorial is java"; //replacing all the occurencres of is with by. System.out.println(str1.replaceAll("is", "by")); //removing all the whitespaces. System.out.println(str1.replaceAll("\\s","")); } }
Output:
String Tutorial by java StringTutorialisjava.
Remember that, it throws PatternSyntaxException if the regular expression syntax is invalid. Let’s see the example.
class StringReplace{ public static void main(String args[]){ String str1 = "String Tutorial is javastudypoint"; //throws PatternSyntaxException System.out.println(str1.replaceAll("[", "")); } }
Output:
3. replaceFirst() method in Java
This method is applied to replaces the first substring of this string that meets the given regular expression with the given replacement. It throws PatternSyntaxException if the regular expression is invalid.
Syntax:
The syntax of the replaceFirst() method is given below:
public String replaceFirst(String regex, String replacement)
Example of Java replaceFirst() method:
class StringReplace{ public static void main(String args[]){ String str1 = "String Tutorial by author"; //repalces first occuerence of author with Prashant. System.out.println(str1.replaceFirst("author","Lavanya")); } }
Output:
String Tutorial by Lavanya
replace() vs replaceAll() vs replaceFirst()
The main difference between replace(), replaceAll(), and replaceFirst() methods are listed below:
- The difference between replace() and replaceAll() method is that the replace() method replaces all the events of the old char with the new char whereas the replaceAll() method replaces all the events of an old string with the new string.
- Fundamentally, replace() operates with replacing chars and replaceAll() operates with replacing part of strings.
- When it comes to the difference between replaceFirst() and replaceAll() method. The replaceFirst() replaces the first occurrence while replaceAll() replaces all the occurrences.