Java URL Decode urldecode(String str)

Here you can find the source of urldecode(String str)

Description

The opposite function of urlencode().

License

Apache License

Declaration

public static String urldecode(String str) 

Method Source Code

//package com.java2s;
/*/*  w  w w .  j  a  v a  2  s .  com*/
 * Copyright 2010 the original author or authors.
 * Copyright 2010 SorcerSoft.org.
 *  
 * 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 {
    /**
     * The opposite function of urlencode().
     */
    public static String urldecode(String str) {
        int len = str.length();
        StringBuffer newstr = new StringBuffer(len);

        for (int i = 0; i < len; i++) {
            if (str.charAt(i) == '+') {
                newstr.append(' ');
            } else if (str.charAt(i) == '%') {
                newstr.append(dd2c(str.charAt(i + 1), str.charAt(i + 2)));
                i += 2;
            } else {
                newstr.append(str.charAt(i));
            }
        }
        return newstr.toString();
    }

    /**
     * Transform two hex digits to corresponding char.
     */
    public static char dd2c(char d1, char d2) {
        return (char) (Character.digit(d1, 16) * 16 + Character.digit(d2,
                16));
    }
}

Related

  1. unescape(String s)
  2. unescape(String string)
  3. unescape(String theString)
  4. urlDecode(String s)
  5. urlDecode(String scope)
  6. urlDecode(String str)
  7. URLDecode(String str)
  8. urlDecode(String text)
  9. urlDecodeBytes(String key)