Replaceall () slash usage

Source: Internet
Author: User


The Java replaceall () method uses four backslash to represent a backslash.


For example, str1 = "AA \ BBB"; str2 = "AA 'bbb ";


To replace it with str1 = "AA \ BBB"; str2 = "AA \ 'bbb ";


It must be replaced as follows:


Str1 = str1.replaceall ("\\\\","\\\\\\\\");

Str2 = str2.replaceall ("'","\\\\'");


The reason is as follows:


The string replaceall () method is actually matched using regular expression rules,


\\\\, The Java Parsing is \ to the regular expression, the regular expression is converted again, convert \ \


That is to say, the regular expression must be used in Java to indicate a \. Four \


If you want to represent \, you need to write 8 \



Therefore, if it is written as str1 = str1.replaceall ("\\","\\\\");


A regular expression error is reported.


This article from "Yu Hong produced" blog, please be sure to keep this source http://5fresh.blog.51cto.com/5472694/1541570

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.