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

  1. public class StringReplaceExample1{  
  2. public static void main(String args[]){  
  3. String str="javainsimpleway";  
  4. String replacedString=str.replace('a','@');//replaces all occurrences of 'a' to '@'  
  5. System.out.println(replacedString);
  6. }
  7. }
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

  1. public class StringReplaceExample2{  
  2. public static void main(String args[]){  
  3. String str="java is very easy to learn from very good website javainsimpleway";  
  4. String replacedString=str.replace(“very”,”so much”);//replaces all occurrences of very to so much
  5. System.out.println(replacedString);
  6. }
  7. }
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

  1. public class StringReplaceExample3{  
  2. public static void main(String args[]){  
  3. String str="aaa";  
  4. String replacedString=str.replace(“aa”,”b”);//replaces all occurrences of ‘aa’ to ‘b’
  5. System.out.println(replacedString);
  6. }
  7. }
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

  1. public class StringReplaceAllExample1{  
  2. public static void main(String args[]){  
  3. String str=”javainsimpleway";  
  4. String replacedString=str.replaceAll(“a”,”@”);//replaces all occurrences of ‘a’ to ‘@’
  5. System.out.println(replacedString);
  6. }
  7. }
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

  1. public class StringReplaceAllExample2{  
  2. public static void main(String args[]){  
  3. String str="java is very easy to learn from very good website javainsimpleway";  
  4. String replacedString=str.replaceAll(“very”,”so much”);//replaces all occurrences of very to so much
  5. System.out.println(replacedString);
  6. }
  7. }
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.

  1. public class StringReplaceAllRegEx{  
  2. public static void main(String args[]){  
  3. String str="java 123 is 456 very 789 easy";  
  4. String replacedString=str.replaceAll([0-9]+”,””);//remove all numbers from the string
  5. System.out.println(replacedString);
  6.  
  7. replacedString=str.replaceAll([a-zA-Z]+”,"java");//replace each word having alphabets with ‘java’
  8. System.out.println(replacedString);
  9.  
  10. }
  11. }
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

  1. public class StringReplaceFirst{  
  2. public static void main(String args[]){  
  3. String str=This is a javainsimpleway website";  
  4. String replacedString=str.replaceFirst(“is”,””); //replaces first occurrence of ‘is’ to ‘ ’
  5. System.out.println(replacedString);
  6. }
  7. }
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”.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on