Matcher replaceFirst(String) Method in Java with Examples Last Updated : 10 Nov, 2021 Comments Improve Suggest changes Like Article Like Report 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 Comment More infoAdvertise with us Next Article Matcher replaceFirst(String) Method in Java with Examples K Kirti_Mangal Follow Improve Article Tags : Java Java - util package Java-Functions Java-Matcher Practice Tags : Java Similar Reads Matcher replaceAll(String) method in Java with Examples The replaceAll(String) method of Matcher Class behaves as a append-and-replace method. This method reads the input string and replace it with the matched pattern in the matcher string. Syntax: public String replaceAll(String stringToBeReplaced) Parameters: This method takes a parameter stringToBeRep 2 min read Matcher quoteReplacement(String) method in Java with Examples The quoteReplacement(String string) method of Matcher Class is used to get the replacement String literal of the String passed as parameter. This String literal acts as the parameter for the replace methods. Hence quoteReplacement() method acts as the intermediate in the replace methods. Syntax: pub 2 min read Matcher start(String) method in Java with Examples The start(String string) method of Matcher Class is used to get the start index of the match result already done, from the specified string. Syntax: public int start(String string) Parameters: This method takes a parameter string which is the String from which the start index of the matched pattern 2 min read String replace() method in Java with Examples The String replace() method returns a new string after replacing all the old characters/CharSequence with a given character/CharSequence. Example:Return a new string where all " o" characters are replaced with "p" character: Java // Java program to demonstrate // the replace() method public class Ma 4 min read Matcher toString() method in Java with Examples The toString() method of Matcher Class is used to get the String representation of this matcher. This method is derived from the Object Class and behaves in the similar way. Syntax: public String toString() Parameters: This method takes no parameters. Return Value: This method returns a String value 2 min read Like