Special HTML character encoding

Using Java on Android I'm struggling to convert some special html characters.

So far I have tried:

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 

The correct output should be => 2.00 per month³

+4
source share
2 answers

Your string is encoded in ISO-8859-1 , so ASCII and UTF-8 will not work.

 String myString = "%A32.00%20per%20month%B3"; URLDecoder.decode(myString, "ISO-8859-1"); // output: £2.00 per month³ 
+8
source
 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); } 

At the output, I get:

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

Are you sure that %A32.00%20per%20month%B3 correct?

+2
source

Source: https://habr.com/ru/post/1387230/


All Articles