HTML特殊字符解码

时间:2011-12-20 16:47:20

标签: java android character-encoding

在Android上使用Java我很难转换几个html特殊字符。

到目前为止,我已经尝试过:

String myString = "%A32.00%20per%20month%B3";

Html.fromHtml(myString).toString(); => %A32.00%20per%20month%B3
URLDecoder.decode(myString) => �2.00 per month�
URLDecoder.decode(myString, "UTF-8") => �2.00 per month�
URLDecoder.decode(myString, "ASCII") => �2.00 per month�
org.apache.commons.lang.StringEscapeUtils.unescapeHtml4(myString) => %A32.00%20per%20month%B3

正确的输出应该是=>每月£2.00³

2 个答案:

答案 0 :(得分:8)

您的字符串以ISO-8859-1编码,因此ASCII和UTF-8不起作用。

String myString = "%A32.00%20per%20month%B3";
URLDecoder.decode(myString, "ISO-8859-1");
// output: £2.00 per month³

答案 1 :(得分:2)

public static void main(String[] args) throws UnsupportedEncodingException {
    String before = "£2.00 per month³";
    String encoded = URLEncoder.encode(before, "UTF-8");
    String decoded = URLDecoder.decode(encoded, "UTF-8");
    System.out.println(encoded);
    System.out.println(decoded);
}

在输出中我得到:

%C2%A32.00+per+month%C2%B3
£2.00 per month³

您确定%A32.00%20per%20month%B3是否正确?

相关问题