繁体   English   中英

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

[英]I can't get replace / replaceAll to work

我需要显示数据库中应该显示的数据:

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

但是,相反,我得到了这样的数据:

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

因此,我需要更改& 回到& 我使用replaceAll和Java的String类中的replace方法。 但这没有用。 我什至检查了String是否具有&是否使用indexOf方法,但它甚至似乎都没有捕捉到甚至看不到& 标志。

我的代码:

// 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";

结果是

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

我输入和使用replace / replaceAll的方式有什么问题吗? 谢谢你的回答。

我建议对StringEscapeUtils.unescapeHtml()使用Apache Commons Lang

将编码后的字符输入字符串,然后将其吐出解码。

暂无
暂无

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

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