Return an encoded String for url from value - Java java.lang

Java examples for java.lang:String Unicode

Description

Return an encoded String for url from value

Demo Code


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

import java.net.URLEncoder;

public class Main {
    public static void main(String[] argv) throws Exception {
        String value = "java2s.com";
        System.out.println(urlEncode(value));
    }/*from  w  w w. j a  va  2  s .c o  m*/

    /**
     * Return an encoded String for url from value
     * @param value string to encode
     * @return encoded string
     */
    public static String urlEncode(String value) {
        if (value == null)
            return value;
        try {
            return URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials