Java URI Decode decodeURIComponent(String s, String charset)

Here you can find the source of decodeURIComponent(String s, String charset)

Description

Decodes the passed String using an algorithm that's compatible with JavaScript's decodeURIComponent function.

License

Open Source License

Parameter

Parameter Description
s The encoded String to be decoded
charset a parameter

Return

the decoded String

Declaration

public static String decodeURIComponent(String s, String charset) 

Method Source Code


//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class Main {
    /**//from www  .  java 2s  . co  m
     * Decodes the passed UTF-8 String using an algorithm that's compatible with
     * JavaScript's <code>decodeURIComponent</code> function. Returns
     * <code>null</code> if the String is <code>null</code>.
     *
     * @param s The UTF-8 encoded String to be decoded
     * @return the decoded String
     */
    public static String decodeURIComponent(String s) {
        return decodeURIComponent(s, "UTF-8");
    }

    /**
     * Decodes the passed String using an algorithm that's compatible with
     * JavaScript's <code>decodeURIComponent</code> function. Returns
     * <code>null</code> if the String is <code>null</code>.
     *
     * @param s The encoded String to be decoded
     * @param charset
     * @return the decoded String
     */
    public static String decodeURIComponent(String s, String charset) {
        if (s == null) {
            return null;
        }

        String result = null;

        try {
            result = URLDecoder.decode(s, charset);
        }

        // This exception should never occur.
        catch (UnsupportedEncodingException e) {
            result = s;
        }

        return result;
    }
}

Related

  1. decodeURI(String s)
  2. decodeURI(String str, boolean fullUri)
  3. decodeUri(String string)
  4. decodeURI(String uri)
  5. decodeURI(URI uri)
  6. decodeURItoMap(String URIstring)
  7. uriDecode(String encoded)
  8. uriDecodePath(String path)
  9. uriDecoding(String str)