Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Matcher quoteReplacement(String) method in Java with Examples

  • Last Updated : 26 Nov, 2018

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:

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.

public static String quoteReplacement(String string)

Parameters: This method takes a parameter string which is the String to be replaced in the Matcher.

Return Value: This method returns a String literal which is the replacement String for the matcher.



Below examples illustrate the Matcher.quoteReplacement() method:

Example 1:




// Java code to illustrate quoteReplacement() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the regex to be checked
        String regex = "Geek";
  
        // Create a pattern from regex
        Pattern pattern = Pattern.compile(regex);
  
        // Get the String to be matched
        String
            stringToBeMatched
            = "GeeksForGeeks Geeks for For Geeks Geek";
  
        // Create a matcher for the input String
        Matcher matcher
            = pattern
                  .matcher(stringToBeMatched);
  
        // Get the String to be replaced
        String stringToBeReplaced = "Geeks";
  
        // Get the String literal
        // using quoteReplacement() method
        System.out.println(
            matcher
                .quoteReplacement(stringToBeReplaced));
    }
}
Output:
Geeks

Example 2:




// Java code to illustrate quoteReplacement() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the regex to be checked
        String regex = "FGF";
  
        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);
  
        // Get the String to be matched
        String
            stringToBeMatched
            = "GFGFGFGFGFGFGFGFGFG FGF GFG GFG FGF";
  
        // Create a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
  
        // Get the String to be replaced
        String stringToBeReplaced = "GFG";
  
        // Get the String literal
        // using quoteReplacement() method
        System.out.println(
            matcher
                .quoteReplacement(stringToBeReplaced));
  
        System.out.println(
            matcher
                .replaceAll(
                    matcher
                        .quoteReplacement(
                            stringToBeReplaced)));
    }
}
Output:
GFG
GGFGGGFGGGFGGGFGGFG GFG GFG GFG GFG

Reference: Oracle Doc




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!