Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Matcher replaceFirst(String) Method in Java with Examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The replaceFirst() method of Matcher Class behaves as an append-and-replace method. This method reads the input string and replaces it with the first matched pattern in the matcher string. 

Syntax: 

public String replaceFirst(String stringToBeReplaced)

Parameters: The string to be replaced that is the String to be replaced in the matcher.

Return Type: A string with the target string constructed by replacing the string.

Example 1:

Java




// Java Program to Illustrate replaceFirst() Method
// of Matcher class
 
// Importing required classes
import java.util.regex.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Getting the regex to be checked
        String regex = "(Geeks)";
 
        // Creating a pattern from regex
        Pattern pattern = Pattern.compile(regex);
 
        // Getting the String to be matched
        String stringToBeMatched
            = "GeeksForGeeks Geeks for For Geeks Geek";
 
        // Creating a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
 
        // Displaying the string to be matched
        // before replacing
        System.out.println("Before Replacement: "
                           + stringToBeMatched);
 
        // Getting the string to be replaced
        String stringToBeReplaced = "GFG";
        StringBuilder builder = new StringBuilder();
 
        // Replacing every matched pattern with the target
        // string using replaceFirst() method
        System.out.println(
            "After Replacement: "
            + matcher.replaceFirst(stringToBeReplaced));
    }
}


Output: 

Before Replacement: GeeksForGeeks Geeks for For Geeks Geek
After Replacement: GFGForGeeks Geeks for For Geeks Geek

 

Example 2:

Java




// Java Program to Illustrate replaceFirst() Method
 
// Importing required classes
import java.util.regex.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Getting the regex to be checked
        String regex = "(FGF)";
 
        // Creating a pattern from regex
        Pattern pattern = Pattern.compile(regex);
 
        // Getting the string to be matched
        String stringToBeMatched = "FGF FGF FGF FGF";
 
        // Creating a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
 
        // Printing string on console
        // before replacement
        System.out.println("Before Replacement: "
                           + stringToBeMatched);
 
        // Getting the string to be replaced
        String stringToBeReplaced = "GFG";
        StringBuilder builder = new StringBuilder();
 
        // Replacing every matched pattern with target
        // string using replaceFirst() method and printing
        // the string to be replaced
        System.out.println(
            "After Replacement: "
            + matcher.replaceFirst(stringToBeReplaced));
    }
}


Output: 

Before Replacement: FGF FGF FGF FGF
After Replacement: GFG FGF FGF FGF

 


My Personal Notes arrow_drop_up
Last Updated : 10 Nov, 2021
Like Article
Save Article
Similar Reads
Related Tutorials