Java String Unescape unescape(String s)

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

Description

Decodes a string escaped by the Internet Explorer's Javascript escape() method to a Unicode String.

License

Apache License

Declaration

public static String unescape(String s) 

Method Source Code

//package com.java2s;
/**//  w  w w. j a  v  a  2s .  c  om
 * Copyright 2009 Welocalize, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.
 * 
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 */

public class Main {
    /**
     * Decodes a string escaped by the Internet Explorer's Javascript escape()
     * method to a Unicode String. IE's version encodes chars above 255 as
     * %uXXXX and does not map '+' to ' '.
     */
    public static String unescape(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }

        StringBuffer sb = new StringBuffer(s.length());

        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);

            switch (c) {
            case '%':
                try {
                    if (s.charAt(i + 1) == 'u') {
                        sb.append((char) Integer.parseInt(s.substring(i + 2, i + 6), 16));
                        i += 5;
                    } else {
                        sb.append((char) Integer.parseInt(s.substring(i + 1, i + 3), 16));
                        i += 2;
                    }
                } catch (Exception e) {
                    sb.append(c);
                }
                break;
            default:
                sb.append(c);
                break;
            }
        }

        return sb.toString();
    }
}

Related

  1. unescape(String oldStr)
  2. unescape(String original, char[] spec)
  3. unescape(String property)
  4. unescape(String quoted)
  5. unescape(String s)
  6. unEscape(String s)
  7. unescape(String s)
  8. unescape(String s)
  9. unescape(String s)