Java URLDecoder.decode(String s, String enc)

Syntax

URLDecoder.decode(String s, String enc) has the following syntax.

public static String decode(String s,  String enc)   throws UnsupportedEncodingException

Example

In the following code shows how to use URLDecoder.decode(String s, String enc) method.


/*from w ww .  j a  v a  2  s .  c  om*/
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Main {
  public static void main(String[] argv) throws Exception {
    String line = URLEncoder.encode("name1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");

    String[] pairs = line.split("\\&");
    for (int i = 0; i < pairs.length; i++) {
      String[] fields = pairs[i].split("=");
      String name = URLDecoder.decode(fields[0], "UTF-8");
      System.out.println(name);
      String value = URLDecoder.decode(fields[1], "UTF-8");
      System.out.println(value);
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.net »




CookieManager
CookiePolicy
CookieStore
DatagramPacket
DatagramSocket
HttpCookie
HttpURLConnection
InetAddress
JarURLConnection
MulticastSocket
ServerSocket
Socket
SocketAddress
URI
URL
URLConnection
URLDecoder
URLEncoder