简体   繁体   English

我无法替换/替换全部工作

[英]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. 我使用replaceAll和Java的String类中的replace方法。 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 & 我什至检查了String是否具有&是否使用indexOf方法,但它甚至似乎都没有捕捉到甚至看不到& 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? 我输入和使用replace / replaceAll的方式有什么问题吗? Thank you for the answer. 谢谢你的回答。

I would recommend using Apache Commons Lang for the StringEscapeUtils.unescapeHtml(). 我建议对StringEscapeUtils.unescapeHtml()使用Apache Commons Lang

Feed it your string with the encoded characters, and it should spit it out decoded. 将编码后的字符输入字符串,然后将其吐出解码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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