In this tutorial, we will discuss replace()
, replaceFirst()
and replaceAll()
methods. All of these Java String methods are mainly used for replacing a part of String with another String.
Java String replace method signature
- String replace(char oldChar, char newChar): It replaces all the occurrences of a oldChar character with newChar character. For e.g.
"pog pance".replace('p', 'd')
would returndog dance
. - String replaceFirst(String regex, String replacement): It replaces the first substring that fits the specified regular expression with the replacement String.
PatternSyntaxException
if the specified regular expression(regex) is not valid. - String replaceAll(String regex, String replacement): It replaces all the substrings that fits the given regular expression with the replacement String.
Java String replace() Method example
In the following example we are have a string str
and we are demonstrating the use of replace() method using the String str. We have replaced all the occurrences of char ‘o’ with char ‘p’. In the second print statement we have replaced all the occurrences of char ‘i’ with the char ‘K’.
Here we are displaying the modified string using print statements but we didn’t actually changed the string str
, to achieve that we need to assign the returned string of replace() method in a string and then that string can have the permanent changes.
public class JavaExample{ public static void main(String args[]){ String str = new String("Site is BeginnersBook.com"); System.out.print("String after replacing all 'o' with 'p' :" ); System.out.println(str.replace('o', 'p')); System.out.print("String after replacing all 'i' with 'K' :" ); System.out.println(str.replace('i', 'K')); } }
Output:
Java String replaceFirst() Method example
In the following example we are demonstrating the use of replaceFirst() method. This method replaces the part of a string with a new specified string. The difference between replaceFirst() and replaceAll() method is that the replaceFirst() replaces the first occurrence while replaceAll() replaces all the occurrences.
public class JavaExample{ public static void main(String args[]){ String str = new String("Site is BeginnersBook.com"); System.out.print("String after replacing com with net :" ); System.out.println(str.replaceFirst("com", "net")); System.out.print("String after replacing Site name:" ); System.out.println(str.replaceFirst("Beginners(.*)", "XYZ.com")); } }
Output:
Java String replaceAll() Method Example
In the following example we are using replaceAll()
method to replace all the occurrences of a given substring with the new string.
The difference between replace() and replaceAll() method is that the replace()
method replaces all the occurrences of old char with new char while replaceAll()
method replaces all the occurrences of old string with the new string. Basically replace() works with replacing chars and replaceAll() works with replacing part of strings.
public class JavaExample{ public static void main(String args[]){ String str = new String("My .com site is BeginnersBook.com"); System.out.print("String after replacing all com with net: " ); System.out.println(str.replaceAll("com", "net")); System.out.print("Replacing whole String: " ); System.out.println(str.replaceAll("(.*)Beginners(.*)", "Welcome")); } }
Output:
Case-insensitive replaceAll()
In the above example, we saw a case sensitive replace. We can do a case insensitive replace like this:
public class JavaExample { public static void main(String args[]) { String str = "Hello Ram, hello Steve"; String repStr = str.replaceAll("Hello", "Hi"); //(?i) is for the case insensitive replace String repStr2 = str.replaceAll("(?i)hello", "Hi"); System.out.println("Input string: " + str); System.out.println("Case sensitive replaceAll: " + repStr); System.out.println("Case insensitive replaceAll : " + repStr2); } }
Output:
Chenna Harish says
i want apostrophe like ‘gugjhg’ and jhjhh’dd these type of insertion in database using string.replace() method in java