Java slash substitution

Source: Internet
Author: User

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

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.