Java String Unescape unescape(String x)

Here you can find the source of unescape(String x)

Description

This finds any '%xx' and converts to the equivilent char.

License

Open Source License

Parameter

Parameter Description
x operate on this String

Return

original String.

Declaration

static public String unescape(String x) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*ww  w  . j a va  2  s. co m*/
     * This finds any '%xx' and converts to the equivilent char. Inverse of escape().
     *
     * @param x operate on this String
     * @return original String.
     */
    static public String unescape(String x) {
        if (x.indexOf('%') < 0) {
            return x;
        }

        // gotta do it
        char[] b = new char[2];
        StringBuilder sb = new StringBuilder(x);
        for (int pos = 0; pos < sb.length(); pos++) {
            char c = sb.charAt(pos);
            if (c != '%') {
                continue;
            }
            if (pos >= sb.length() - 2) { // malformed - should be %xx
                return x;
            }
            b[0] = sb.charAt(pos + 1);
            b[1] = sb.charAt(pos + 2);
            int value;
            try {
                value = Integer.parseInt(new String(b), 16);
            } catch (NumberFormatException e) {
                continue; // not a hex number
            }
            c = (char) value;
            sb.setCharAt(pos, c);
            sb.delete(pos + 1, pos + 3);
        }

        return sb.toString();
    }
}

Related

  1. unescape(String value)
  2. unescape(String value)
  3. unescape(String value)
  4. unEscape(String value)
  5. unescape(String value)
  6. unescape(StringBuilder escapedText, int index)
  7. unEscapeString(final String src)
  8. unescapeString(String escapedString)
  9. unescapeString(String iccProfileSrc)