The difference between Java forward slash and back slash

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.