简体   繁体   中英

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

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 encodes 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) + ";");

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