Reputation: 15
I want to find a substitute for javascript unescape in java
I have tried this and it has not worked for me
String cadena = "%u0032%u0038";
System.out.println(URLDecoder.decode(cadena, "UTF-8"));
Upvotes: 1
Views: 180
Reputation: 159165
Implement the unescape()
method yourself.
In Java 11+, you can do it like this:
public static String unescape(String s) {
return Pattern.compile("%u([0-9A-Fa-f]{4})|%([0-9A-Fa-f]{2})").matcher(s).replaceAll(m ->
Character.toString(Integer.parseInt(m.group(m.start(1) != -1 ? 1 : 2), 16)));
}
In earlier Java versions, use:
public static String unescape(String s) {
StringBuffer buf = new StringBuffer();
Matcher m = Pattern.compile("%u([0-9A-Fa-f]{4})|%([0-9A-Fa-f]{2})").matcher(s);
while (m.find())
m.appendReplacement(buf, new String(new int[] { Integer.parseInt(m.group(m.start(1) != -1 ? 1 : 2), 16) }, 0, 1));
return m.appendTail(buf).toString();
}
Test
String cadena = "%u0032%u0038";
System.out.println(unescape(cadena));
Output
28
Upvotes: 2