0

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Jordan
  • 1,875
  • 2
  • 17
  • 24
  • 1
    Possible duplicate of [Java: How to decode HTML character entities in Java like HttpUtility.HtmlDecode?](http://stackoverflow.com/questions/994331/java-how-to-decode-html-character-entities-in-java-like-httputility-htmldecode) – Peyman Mohamadpour Feb 15 '16 at 04:10
  • Related answers to the original question: [replace-non-ascii-character-from-string](http://stackoverflow.com/questions/8519669/replace-non-ascii-character-from-string) and [remove-ascii-symbol-from-string](http://stackoverflow.com/questions/26525752/remove-ascii-symbol-from-string) – lepe Dec 13 '16 at 05:54

1 Answers1

5

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"{
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249