Let's start by outlining their three usage:
replace (charsequence target, charsequence replacement) replaces all target with replacement, and two parameters are strings.
ReplaceAll (string regex, string replacement) replaces all regex matches with replacement, the regex is clearly a regular expression, replacement is a string.
Replacefirst (string regex, string replacement) , Basic and ReplaceAll, the difference being to replace only the first match.
The next simple requirement is to replace a in the source string with \a, which is as follows:
System.out.println ("Abac". Replace ("A", "\\a")); \ab\ac
System.out.println ("Abac". ReplaceAll ("A", "\\a");//abac
System.out.println ("Abac"). Replacefirst ("A", "\\a")); Abac
The result was a surprise, and with so many years of substitution, it was a bit of a blindfold.
The source string is "Abac", and then we find "a" and replace it with \a, because \ is a Java escape character, so the expression \a must be written "\\a", and the first backslash escapes the second backslash into a normal string.
Three substitution expressions, only the result of the first replace function is correct, what is the problem?
ReplaceAll and Replacefirst require the first argument to be a regular expression, and "a" can be understood as either a string a or a regular expression A, so the first argument is fine.
The problem is on the second argument, if the reader reads the annotation of the ReplaceAll function carefully, you will find the following description:
Note so backslashes (\) and dollar signs ($) in the replacement string could cause the results to is different than if it were being treated as a literal replacement string; Matcher.replaceall. Use Java.util.regex.Matcher.quoteReplacement to suppress the special meaning of this characters, if desired.
Since the first argument of ReplaceAll and Replacefirst is regular, we can do a little trick in the second argument, such as having a requirement to replace a in the source string with a character next to a, followed by the following code:
System.out.println ("Abac". ReplaceAll ("A (\\w)", "$1$1")); BBCC
System.out.println ("Abac". Replacefirst ("A (\\w)", "$1$1"));//bbac
The regular meaning assumes the reader can understand, can see, in the second parameter, can use the $ symbol to get the content of the grouping, in this example, the contents of the first grouping are taken, that is, the character immediately after a.
Therefore, the $ symbol has a special meaning in the second argument, and the write error will be the same:
System.out.println ("Abac". ReplaceAll ("A (\\w)", "$")); Exception in thread ' main ' java.lang.StringIndexOutOfBoundsException:String index out of Range:1
What if I want to replace it with $? This requires an escape character:
System.out.println ("Abac". ReplaceAll ("A", "\\$")); $b $c
Here, the reader may suddenly realize that the backslash also has a special meaning (escape) in the second argument, so if we want to express a backslash, we must escape it again:
System.out.println ("Abac". ReplaceAll ("A", "\\\\a")); \ab\ac
System.out.println ("Abac". Replacefirst ("A", "\\\\a"));//\abac
In a nutshell, thebackslash at the front of "\\\\a" escapes the backslash below, allowing the backslash to be the normal string, so that the string seen in Java memory is "\\a", and then the ReplaceAll function is then used to escape the back slash , to express the back backslash is a normal string, not to escape $, the final memory of the string is "\a", so that you can successfully replace a \a.
Summarize
Escaping the problem is really tangled, through this article, the author hopes that the reader later use these functions, can stay awake, can realize the special characters in the parameters, avoid writing time bombs. The above is the entire content of this article, I hope that the study and work can help, if there are questions can be exchanged message.