开发者

How to encode a non-special character to HTML entity in Java

开发者 https://www.devze.com 2023-04-11 07:42 出处:网络
The following code: org.apache.commons.lang.StringEscapeUtils.unescapeHtml(\"Hello World\");

The following code:

org.apache.commons.lang.StringEscapeUtils.unescapeHtml("Hello World");

gives:

Hello World

But I'd like to know how to get back to the decoded string from "Hello World". I have tried the escapeHtml method, but this only enco开发者_开发百科des special characters.


But I'd like to know how to get back to the decoded string from "Hello World". I have tried the escapeHtml method, but this doesn't do anything useful.

Not true about "anything useful"; if your test string contained HTML special characters like <,>,&, the function would've turned it into &lt; &gt; and &amp; (and change other upper ISO8859-1 codes into entities).

If you need to encode it back to Unicode entity format, just iterate through the String codepoints:

for (int i = 0; i < str.length(); i++)
    System.out.print("&#" + str.codePointAt(i) + ";");
0

精彩评论

暂无评论...
验证码 换一张
取 消