转换HTML转义字符串为纯统一code / ASCII字符串、HTML、纯统一、ASCII

2023-09-05 04:49:38 作者:三生石畔我独怜

可能重复:   Java:如何去在Java中code HTML字符实体像HttpUtility.HtmlDe code?

有一个Java / Android的方式转换成HTML转义的字符串(如&放大器; Ouml; &放大器;大街)回到它们的ASCII / UNI code再presentations(如 0 SS )?

is there a Java/Android way to convert HTML-escaped strings (such as Ö or ß) back to their ASCII/Unicode representations (such as Ö or ß)?

我当然不想做一个简单的字符串替换,并尝试只用所有的HTML转义序列的存在,我想有一个随时可以使用的解决方案?

I of course do not want to do a simple string-replacement and try with just every HTML-escape-sequence that exists, I'd guess there is a ready-to use solution?

谢谢!

推荐答案

使用这样的:

import org.apache.commons.lang.StringEscapeUtils;

public class StringEscapeUtilsTrial {
public static void main(String[] args) {
    String strHTMLInput = "<p>MyName<p>";
    String strEscapeHTML = StringEscapeUtils.escapeHtml(strHTMLInput);
    String strUnEscapeHTML = StringEscapeUtils.unescapeHtml(strEscapeHTML);
    System.out.println("Escaped HTML >>> " + strEscapeHTML);
    System.out.println("UnEscaped HTML >>> " + strUnEscapeHTML);
    }
}