Chen Li Yong
Chen Li Yong

Reputation: 6097

I can't get replace / replaceAll to work

I need to show data from database that supposed to be shown:

in the html: Behälter
in the browser: Behälter

but instead, I got data like this:

in the html: Behälter
in the browser: Behälter

So I need to change the & back to &. I use replaceAll and replace method from the Java's String class. But it didn't work. I even check whether the String has & or not using indexOf method, but it didn't even seems to catch or even see the & sign.

My code:

// supposed the value returned by the getObject function is "Behälter"
String text = (String)getObject("value");

if (text.indexOf("&") >= 0) text = "abc" + text;

text = text.replace("&", "&");
text = text.replaceAll("&", "&");

if (text.indexOf("&") >= 0) text = text + "def";
text = text + "xyz";

The result is

in the html: Behälterxyz
in the browser: Behälterxyz

Is there anything wrong with how I type and use replace / replaceAll? Thank you for the answer.

Upvotes: 0

Views: 163

Answers (1)

JC.
JC.

Reputation: 650

I would recommend using Apache Commons Lang for the StringEscapeUtils.unescapeHtml().

Feed it your string with the encoded characters, and it should spit it out decoded.

Upvotes: 1

Related Questions