VOOZH about

URL: https://www.geeksforgeeks.org/java/matcher-replacefirststring-method-in-java-with-examples/

⇱ Matcher replaceFirst(String) Method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Matcher replaceFirst(String) Method in Java with Examples

Last Updated : 11 Jul, 2025

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:


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

 

Example 2:


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

 
Comment