簡體   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