android UTF8 encoding from received string

前端 未结 3 1822
清歌不尽
清歌不尽 2021-02-14 02:06

I am receiving a string that is not properly encoded like mystring%201, where must be mystring 1. How could I replace all characters that could be inte

相关标签:
3条回答
  • 2021-02-14 02:31

    You can use the URLDecoder.decode() function, like this:

    String s = URLDecoder.decode(myString, "UTF-8");
    
    0 讨论(0)
  • 2021-02-14 02:40

    Looks like your string is partially URL-encoded, so... how about this:

    try {
     System.out.println(URLDecoder.decode("mystring%201", "UTF-8"));
    } catch(UnsupportedEncodingException e) {
     e.printStackTrace();
    }
    
    0 讨论(0)
  • 2021-02-14 02:44

    I am receiving a string that is not properly encoded like "mystring%201

    Well this string is already encoded, you have to decode:

    String sDecoded = URLDecoder.decode("mystring%201", "UTF-8");
    

    so now sDecoded must have the value of "mystring 1".

    The "Encoding" of String:

    String sEncoded = URLEncoder.encode("mystring 1", "UTF-8");
    

    sEncoded must have the value of "mystring%201"

    0 讨论(0)
提交回复
热议问题