简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM