64

I am retrieving data from a database, where the field contains a String with HTML data. I want to replace all of the double quotes such that it can be used for parseJSON of jQuery.

Using Java, I am trying to replace the quotes using..

details.replaceAll("\"","\\\"");
  //details.replaceAll("\"","&quote;"); details.replaceAll("\"","&#34");

The resultant string doesn't show the desired change. An O'Reilly article prescribes using Apache string utils. Is there any other way??

Is there a regex or something that I could use?

9 Answers 9

110

Here's how

String details = "Hello \"world\"!";
details = details.replace("\"","\\\"");
System.out.println(details);               // Hello \"world\"!

Note that strings are immutable, thus it is not sufficient to simply do details.replace("\"","\\\""). You must reassign the variable details to the resulting string.


Using

details = details.replaceAll("\"","&quote;");

instead, results in

Hello &quote;world&quote;!
Sign up to request clarification or add additional context in comments.

1 Comment

my string would be like: ABC <a href="fsgdfs"><div id="id1">df</div> ... I want to remove the quotes just to ensure that it gets parsed by JSON. So &quote; might be a btr way for HTML, was the thought.
38

Would not that have to be:

.replaceAll("\"","\\\\\"")

FIVE backslashes in the replacement String.

5 Comments

Yes, if you were to use replaceAll(), that would be the correct way. That's why everyone is saying to use replace() instead. ;)
Just spent the better part of an hour figuring out why my replaceAll() wasn't working. Totally forgot that there was a replace() method too.
Why 3 backslashes using replace() but 5 backslashes using replaceAll()?
I think the answer is in the method signature. replace method in this case is actually replace(CharSequence cs, CharSequence cs1) and replaceAll is replaceAll(String string, String string1). To pass String as parameter you need those extra backslashes.
@vinay no, it's because replaceAll takes a regular expression while replace takes a plain string.
10

I think a regex is a little bit of an overkill in this situation. If you just want to remove all the quotes in your string I would use this code:

details = details.replace("\"", "");

Comments

6

To make it work in JSON, you need to escape a few more character than that.

myString.replace("\\", "\\\\")
    .replace("\"", "\\\"")
    .replace("\r", "\\r")
    .replace("\n", "\\n")

and if you want to be able to use json2.js to parse it then you also need to escape

   .replace("\u2028", "\\u2028")
   .replace("\u2029", "\\u2029")

which JSON allows inside quoted strings, but which JavaScript does not.

2 Comments

I am using jquery 1.5.. Do i require this all?
@MalTec, If your string can contain backslashes, then yes. Consider if your string is '\\"'. If you wrap it in quotes and only escape the quotes, then you get "\\"" which is an invalid string. If your string can contain newlines, then you need to escape those as well. Newlines are not allowed inside strings in JQuery.
3

I know the answer is already accepted here, but I just wanted to share what I found when I tried to escape double quotes and single quotes.

Here's what I have done: and this works :)

to escape double quotes:

    if(string.contains("\"")) {
        string = string.replaceAll("\"", "\\\\\"");
    }

and to escape single quotes:

    if(string.contains("\'")) {
        string = string.replaceAll("\'", "\\\\'");
    }

PS: Please note the number of backslashes used above.

2 Comments

why need five backslash for replaceAll to esacpe double quotes? what is the function of each backslash?
I found string = string.replaceAll("\"",""); already work, output string already remove all double quotes
1
String info = "Hello \"world\"!";
info = info.replace("\"", "\\\"");

String info1 = "Hello "world!";
info1 = info1.replace('"', '\"').replace("\"", "\\\"");

For the 2nd field info1, 1st replace double quotes with an escape character.

Comments

0

This is to remove double quotes in a string.

str1 = str.replace(/"/g, "");
alert(str1);

1 Comment

OP wanted an answer for Java.
0

The following regex will work for both:

  text = text.replaceAll("('|\")", "\\\\$1");

Comments

-1

If you cannot solve the problem with only \* or \* increase the number of backslashes to 5 such as \\" and this will solve your problem

1 Comment

Welcome to stackoverflow. Thank you for attempting to help. But, having duplicate answers make it harder to find the answers. The same thing is expressed by other, in programmatically ways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.