Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Java.lang.String.replace() in Java

  • Difficulty Level : Easy
  • Last Updated : 03 Oct, 2021

This method searches for a specified string or a specified value, or a regex expression by virtue of which has the same suggests returns a new string with related characters. replace() method of strings contains three variants here are three variants of replace() method.

There are 3 types that are frequently used for String.replace() and is also aid in problem solving capabilities.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  • replace()
  • replaceAll()
  • replaceFirst()

#1: String.replace()

This method returns a new string resulting from replacing all occurrences of old characters in the string with new characters. 



Syntax:

public String replace(char oldch, char newch)

Parameters: 

  • The old character
  • The new character

Return Type:

It returns a string derived from this string by replacing every occurrence of oldch with newch.

Example

Java




// Java code to demonstrate the
// Working of  replace() method
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initialising custom input string
        String Str = new String("Welcome to geeksforgeeks");
 
        // Using replace to replace characters
        System.out.print("After replacing all o with T : ");
        System.out.println(Str.replace('o', 'T'));
 
        // Using replace to replace characters
        System.out.print("After replacing all e with D : ");
        System.out.println(Str.replace('e', 'D'));
    }
}
Output
After replacing all o with T : WelcTme tT geeksfTrgeeks
After replacing all e with D : WDlcomD to gDDksforgDDks

#2: String replaceAll()

This method replaces each substring of the string that matches the given regular expression with the given replace_str.



Syntax:

public String replaceAll(String regex, String replace_str)

Parameters:

  • The regular expression to which this string is to be matched.
  • The string which would replace found expression
Return Value
This method returns the resulting String

Example

Java




// Java Program to demonstrate the
// working of  replaceAll()
 
// Main class
public class rep2 {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initialising String
        String Str = new String("Welcome to geeksforgeeks");
 
        // Display message
        System.out.print("Original String : ");
 
        // Printing original string
        System.out.println(Str);
 
        // Display message
        System.out.print(
            "After replacing regex with replace_str : ");
 
        // Using replaceAll to replace regex
        // with replace_str
        System.out.println(
            Str.replaceAll("(.*)geeks(.*)", "ASTHA TYAGI"));
    }
}
Output
Original String : Welcome to geeksforgeeks
After replacing regex with replace_str : ASTHA TYAGI

#3: String replaceFirst()

This method replaces the first substring of this string that matches the given regular expression with the given replace_str. 

Syntax:

public String replaceFirst(String regex, String replace_str)

Parameters

  • The regular expression to which this string is to be matched.
  • The string which would replace found expression.

Return Type: A resulting string

Example:

Java




// Java code to demonstrate the
// working of  replaceFirst()
public class rep3 {
   public static void main(String args[]) {
        
      // Initialising String
      String Str = new String("Welcome to geeksforgeeks");
       
      // original string
      System.out.print("Original String : " );
      System.out.println(Str);
       
      // Using replaceFirst to replace regex with replace_str
      // Replaces 1st occurrence of geeks with ASTHA
      System.out.print("After replacing 1st occurrence of regex with replace_str  : " );
      System.out.println(Str.replaceFirst("geeks", "ASTHA"));
       
   }
}

Output: 

Original String : Welcome to geeksforgeeks
After replacing 1st occurrence of regex with replace_str  : Welcome to ASTHAforgeeks

This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




My Personal Notes arrow_drop_up
Recommended Articles
Page :