Reference Description: There is a forward slash and backslash points, forward slash, is generally called a slash, the symbol is "/"; a backslash symbol "\" refers to a slash (/) in Java there is no particular meaning, that is, a character '/';
The backslash (\) does not, it and the character that follows it, constitute an escape character, such as "\ n" (meaning line break), "\" (For character ' "), etc., so that the character ' \ ' in the string is to be represented by" \ ", for example: If you define a string s =" name\ Sex "is wrong, to define string s =" Name\\sex ";
Reference Note: the "\" representation in a regular expression and the character immediately following it form an escape character (let's name it first) and represent a special meaning, so if you want to represent a backslash in a regular expression, write "\\\\". If you get a matcher,matcher m = pattern.compile ("\"). Matcher ("\") will report an error, you should write Matcher m = pattern.compile ("\\\\"). Matcher ("\") is the correct and matching reference next let's take a look at the replace (charsequence target,charsequence Replacement) method in the String class and ReplaceAll ( The difference between string regex and string replacement methods:
public static void Main (string[] arg) throws Ognlexception {
String s = "Sdf\\a\\aa";
Replace the backslash in S with \
System.out.println (s);
System.out.println (S.replaceall ("\\\\", "\\\\\\\\"));
System.out.println (S.replace ("\", "\\\\"));
}
A reference can see that both of the above return the same substitution result.
The key here is that String.replaceall () is used as a parameter by regular expression. But the string of Java itself has similar processing for escape characters. First, Java interprets "\\\\" as a string (containing two char). Next, because ReplaceAll is a regular expression as an argument, "\ \" is interpreted as a regex. For a regex this represents a character, that is, "\". For the next 8, it will eventually be interpreted as "\ n".
In other words, suppose String.replaceall () is a normal string rather than a regex, so write code: String target = Source.replaceall (' \ n ', ' \\\\ '); It's OK.