简体   繁体   中英

Replace ASCII codes in Java string with character equivalents

I have a Java string like "%7B%22username%22%3A%22test1234%22%7B" , I want to replace all the ascii codes with the character equivalents ( %7B with { , %22 with " , etc.)

Is there a library I could use or some easy way to do this? I want to be able to handle any code from %20 to %FF .

You can use URLDecoder.decode(String, string) . Something like,

String str = "%7B%22username%22%3A%22test1234%22%7B";
try {
    System.out.println(URLDecoder.decode(str, "utf-8"));
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

Which outputs (because you start and end with %7B )

{"username":"test1234"{

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