Replace single quotation marks in a string with double quotes in Java

Source: Internet
Author: User

String regexp = "\";
String str = "' Good '";
System.out.println ("Pre-replacement:" + str);
str = Str.replaceall (regexp, "\" ");

Note that this must be written in str = Str.replaceall (regexp, "\");

cannot be written as Str.replaceall (RegExp, "\"); This will result in a result or ' good ', and single quotes are not replaced because the string class is final decorated and immutable. Therefore, any action on a string object does not alter its contents. and Str.replaceall (RegExp, ""); The equivalent of creating a new string object with a double-quoted string. Then modify the string to refer to str pointing to the new string, and now print the string after the single quote is removed.

System.out.println ("After replacement:" + str);

Replace single quotation marks in a string with double quotes in Java

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: 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.