MalTec
MalTec

Reputation: 1370

Replace all double quotes within String

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?

Upvotes: 64

Views: 258892

Answers (9)

Shohabbos Usmonov
Shohabbos Usmonov

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

Upvotes: -1

priti negi
priti negi

Reputation: 41

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.

Upvotes: 1

elxala
elxala

Reputation: 301

The following regex will work for both:

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

Upvotes: 0

Lakshmana Kumar D
Lakshmana Kumar D

Reputation: 2283

This is to remove double quotes in a string.

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

Upvotes: 0

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

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.

Upvotes: 3

mikhailgarber
mikhailgarber

Reputation: 546

Would not that have to be:

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

FIVE backslashes in the replacement String.

Upvotes: 38

Mike Samuel
Mike Samuel

Reputation: 120586

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.

Upvotes: 6

aioobe
aioobe

Reputation: 421320

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;!

Upvotes: 110

anon
anon

Reputation:

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("\"", "");

Upvotes: 10

Related Questions