Is there any function in Java programming language that is equivalent to JavaScript unescape function? That is, if my input is the string "I%20need%20help%21" the output must be "I need help!", for example.
Thanks!
From my experience URLDecoder.decode
might fail if there are non-ASCII characters in the encoded string. For example this code:
URLDecoder.decode("%u017C", "UTF-8"); // %u017C is the result of running in Javascript escape('ż')
throws the following exception:
Exception in thread "main" java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "u0"
The best way of solving this issue that I know of (though clumsy) is just running Javascript in Java :(
String jsEscapedString = "%u017C";
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
String result = (String) engine.eval("unescape('" + jsEscapedString + "')");
BTW solution inspired by Siva R Vaka's post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With