The Java ReplaceAll () method uses 4 backslashes to represent a backslash
For example str1= "AA\BBB"; str2= "AA ' BBB";
Want to replace str1= "AA\\BBB", str2= "Aa\ ' BBB";
This must be replaced:
STR1 = Str1.replaceall ("\\\\", "\\\\\\\\");
STR2 = Str2.replaceall ("'", "\\\\ '");
The reasons are as follows:
The ReplaceAll () method of String is actually used to match the regular expression rules,
\\\\, java resolves to \ \ to the regular expression, the regular expression again after a conversion, the \ \ conversion into \
That is, Java is to use regular to represent a \. Must be written in 4 \
If you want to represent \ \, then write 8 \
So if written: str1 = Str1.replaceall ("\ \", "\\\\");
It will report the error of the regular expression.
Java replaces the backslash (\) in the string with (\ \)
ReplaceAll is used in the regular expression, so the string escaped once, is escaped once, so a diagonal carry to write 4, with ReplaceAll ("\\\\", "\\\\\\\\");
Java slash substitution