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:
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:
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
String regex = "Geek" ;
Pattern pattern = Pattern.compile(regex);
String
stringToBeMatched
= "GeeksForGeeks Geeks for For Geeks Geek" ;
Matcher matcher
= pattern
.matcher(stringToBeMatched);
String stringToBeReplaced = "Geeks" ;
System.out.println(
matcher
.quoteReplacement(stringToBeReplaced));
}
}
|
Example 2:
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
String regex = "FGF" ;
Pattern pattern
= Pattern.compile(regex);
String
stringToBeMatched
= "GFGFGFGFGFGFGFGFGFG FGF GFG GFG FGF" ;
Matcher matcher
= pattern.matcher(stringToBeMatched);
String stringToBeReplaced = "GFG" ;
System.out.println(
matcher
.quoteReplacement(stringToBeReplaced));
System.out.println(
matcher
.replaceAll(
matcher
.quoteReplacement(
stringToBeReplaced)));
}
}
|
Output:
GFG
GGFGGGFGGGFGGGFGGFG GFG GFG GFG GFG
Reference: Oracle Doc
Last Updated :
26 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...