Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...