Java String replace(), replaceAll() and replaceFirst() Method Example

In this tutorial, I will be sharing about the replace() method, replaceAll() method, replaceFirst() method, what is the difference between replace() and replaceAll() method, and how to replace a character in a String in Java.

Java String class provides three types of replace methods.
1. replace()
2. replaceAll()
3. replaceFirst()

Read Also: String Interview Questions and Answers

Let's dive deep into the methods:

1. Java String replace() method

According to Java docs, Java String replace() method returns a new String by replacing all the occurrences of old char or CharSequence with new char or CharSequence.

Java String class provides two replace() methods:

1. public String replace(char oldChar, char newChar)

Parameters:
oldChar: old Character
newChar: new Character

Returns: This method returns a string by replacing all oldChar with newChar.

2. public String replace(CharSequence target, CharSequence replacement) 
//Introduced in JDK 1.5

Parameters:
target: old sequence of Characters
replacement: new sequence of Characters

Example:

public class JavaHungry {
    public static void main(String args[]) {
      String str1 = new String("Java developers tutorials and coding");
      System.out.println("Original String value is: "+ str1);
      char oldChar = 'a';
      char newChar = 't';
      System.out.println("New String replacing all 'a' with 't' : " +str1.replace(oldChar, newChar));//method 1 example
      CharSequence target = "Java";
      CharSequence replacement = "Python";
      System.out.println("New String replacing 'Java' with 'Python' : " + str1.replace(target, replacement));//method 2 example
    }
}

Output:

Original String value is: Java developers tutorials and coding
New String replacing all 'a' with 't' : Jtvt developers tutoritls tnd coding
New String replacing 'Java' with 'Python' : Python developers tutorials and coding


2. Java String replaceAll() method

According to Java docs, replaceAll() method replaces each substring of a String matching the given regular expression with the given replacement.

 Syntax:

public String replaceAll(String regex, String replacement)

Parameters:
regex : regular expression to which the given String is to be matched
replacement : the string to be substituted for each match

Example:

public class JavaHungry {
    public static void main(String args[]) {
      String str1 = new String("alive is awesome");
      System.out.println("Original String value is: "+ str1);
      String result = str1.replaceAll("a","t");//replacing all occurences of "a" with "t"  
      System.out.println(result); 
      result = str1.replaceAll("is","was");//replacing all occurences of "is" with "was"
      System.out.println(result); 
      result = str1.replaceAll("\\s","");//replacing all occurences of whitespace " " with ""
      System.out.println(result);
    }
}

Output:

Original String value is: alive is awesome
tlive is twesome

alive was awesome
aliveisawesome


3. Java String replaceFirst() method

According to Java docs, replaceFirst() method replaces the first substring of a String matching the given regular expression with the given replacement.

Syntax:

public String replaceFirst(String regex, String replacement)

Parameters:

regex : regular expression to which the given String is to be matched
replacement : the string to be substituted for first match

Example:

public class JavaHungry {
    public static void main(String args[]) {
      String str1 = new String("Be in present");
      System.out.println("Original String value is: "+ str1);
      String result = str1.replaceFirst("e","t");//replacing first occurence of "e" with "t" : 
      System.out.println(result);
    }

Output:

Original String value is: Be in present
Bt in present


4. Difference between replace() and replaceAll() method in Java

1. The first difference between replace() and replaceAll() method is that replace method accepts a pair of char or CharSequence as parameters whereas the replaceAll() method accepts a pair of String as parameters.

2. replace() method does not use regex while replacing old char with new char whereas replaceAll() uses regex to replace old String with new String.

That's all for today, please mention in the comments in case you have any questions related to Java String replace(), replaceAll(), and replaceFirst() method example.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry