Java - Write code to encode string using URL UTF-8 encode

Requirements

Write code to encode string using URL UTF-8 encode

Demo

//package com.book2s;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class Main {
    public static void main(String[] argv) {
        String url = "book2s.com";
        System.out.println(urlEncode(url));
    }/* w ww . j a va2  s. c o  m*/

    public static String urlEncode(String url) {
        try {
            return URLEncoder.encode(url, "utf8");
        } catch (UnsupportedEncodingException e) {
            return url;
        }
    }
}