String replace
We have replace() method in java which returns a new string by replacing all the characters or CharSequences with a specified new character or CharSequence.
There are 4 methods for replacing a string or char value
public String replace(char oldChar, char newChar)
public String replace(CharSequence target,CharSequence replacement)
Public String replaceAll(String regex, String replacement)
Public String replaceFirst(String regex, String replacement)
Let’s discuss each one of them with example
public String replace(char oldChar, char newChar)
This method Returns a new string resulting from replacing
all occurrences of oldChar in this string with newChar
Example
- public class StringReplaceExample1{
- public static void main(String args[]){
- String str="javainsimpleway";
- String replacedString=str.replace('a','@');//replaces all occurrences of 'a' to '@'
- System.out.println(replacedString);
- }
- }
public class StringReplaceExample1{ public static void main(String args[]){ String str="javainsimpleway"; String replacedString=str.replace('a','@');//replaces all occurrences of 'a' to '@' System.out.println(replacedString); } }
public String replace(CharSequence target,CharSequence replacement)
This method returns a new string by replacing each substring that matches the literal target sequence with the specified literal replacement sequence.
Example
- public class StringReplaceExample2{
- public static void main(String args[]){
- String str="java is very easy to learn from very good website javainsimpleway";
- String replacedString=str.replace(“very”,”so much”);//replaces all occurrences of very to so much
- System.out.println(replacedString);
- }
- }
public class StringReplaceExample2{ public static void main(String args[]){ String str="java is very easy to learn from very good website javainsimpleway"; String replacedString=str.replace(“very”,”so much”);//replaces all occurrences of very to so much System.out.println(replacedString); } }
The replacement proceeds from the beginning of the string to the end
Example
- public class StringReplaceExample3{
- public static void main(String args[]){
- String str="aaa";
- String replacedString=str.replace(“aa”,”b”);//replaces all occurrences of ‘aa’ to ‘b’
- System.out.println(replacedString);
- }
- }
public class StringReplaceExample3{ public static void main(String args[]){ String str="aaa"; String replacedString=str.replace(“aa”,”b”);//replaces all occurrences of ‘aa’ to ‘b’ System.out.println(replacedString); } }
One could think of output can be ‘ab’ also but it will be ‘ba’ only as replacement
proceeds from the beginning of the string.
Public String replaceAll(String regex, String replacement)
This method returns a string by replacing each substring of this string that matches the given regular expression with the given replacement.
We can use this method to replace all single characters or all substrings or by using regular expression
Let us see the example for all of these scenarios
Replacing single character
- public class StringReplaceAllExample1{
- public static void main(String args[]){
- String str=”javainsimpleway";
- String replacedString=str.replaceAll(“a”,”@”);//replaces all occurrences of ‘a’ to ‘@’
- System.out.println(replacedString);
- }
- }
public class StringReplaceAllExample1{ public static void main(String args[]){ String str=”javainsimpleway"; String replacedString=str.replaceAll(“a”,”@”);//replaces all occurrences of ‘a’ to ‘@’ System.out.println(replacedString); } }
Replacing substring
- public class StringReplaceAllExample2{
- public static void main(String args[]){
- String str="java is very easy to learn from very good website javainsimpleway";
- String replacedString=str.replaceAll(“very”,”so much”);//replaces all occurrences of very to so much
- System.out.println(replacedString);
- }
- }
public class StringReplaceAllExample2{ public static void main(String args[]){ String str="java is very easy to learn from very good website javainsimpleway"; String replacedString=str.replaceAll(“very”,”so much”);//replaces all occurrences of very to so much System.out.println(replacedString); } }
Replace all using regular expression
This is the most powerful method to replace the matching string in Java as we can pass any regular expression to replace the string rather than a specific matching string.
- public class StringReplaceAllRegEx{
- public static void main(String args[]){
- String str="java 123 is 456 very 789 easy";
- String replacedString=str.replaceAll(“[0-9]+”,””);//remove all numbers from the string
- System.out.println(replacedString);
- replacedString=str.replaceAll(“[a-zA-Z]+”,"java");//replace each word having alphabets with ‘java’
- System.out.println(replacedString);
- }
- }
public class StringReplaceAllRegEx{ public static void main(String args[]){ String str="java 123 is 456 very 789 easy"; String replacedString=str.replaceAll(“[0-9]+”,””);//remove all numbers from the string System.out.println(replacedString); replacedString=str.replaceAll(“[a-zA-Z]+”,"java");//replace each word having alphabets with ‘java’ System.out.println(replacedString); } }
Public String replaceFirst(String regex, String replacement)
This method returns a new string by replacing
first substring of this string that matches the given regular expression with the given replacement.
Example
- public class StringReplaceFirst{
- public static void main(String args[]){
- String str=”This is a javainsimpleway website";
- String replacedString=str.replaceFirst(“is”,””); //replaces first occurrence of ‘is’ to ‘ ’
- System.out.println(replacedString);
- }
- }
public class StringReplaceFirst{ public static void main(String args[]){ String str=”This is a javainsimpleway website"; String replacedString=str.replaceFirst(“is”,””); //replaces first occurrence of ‘is’ to ‘ ’ System.out.println(replacedString); } }
We can see that, it has replaced first occurrence of “is” present in the word “This”.
Hi ,
replacedString=str.replaceAll(“[a-zA-Z]+”,””);//replace each word having alphabets with ‘java’
code should be replaced like
replacedString=str.replaceAll(“[a-zA-Z]+”,”java”);
Thanks Saadhana !!
Updated it.