Java Utililty Methods URL Encode

List of utility methods to do URL Encode

Description

The list of methods to do URL Encode are organized into topic(s).

Method

Stringencode(String s)
Equivalent to #encode(String,String) with second parameter set to the "UTF-8" encoding.
return encode(s, UTF8);
Stringencode(String s)
URL Encodes the given String s using the UTF-8 character encoding.
if (s == null)
    return null;
try {
    return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
return null;
Stringencode(String s)
Like URLEncoder.encode, except translates spaces into %20 instead of +
if (s == null) {
    return "";
try {
    return URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
    return URLEncoder.encode(s).replaceAll("\\+", "%20");
Stringencode(String s)
URL encoding.
try {
    if (s != null)
        return URLEncoder.encode(s, "UTF-8");
    else
        return s;
} catch (UnsupportedEncodingException e) {
    return s;
Stringencode(String s, String enc)
Pass through to the java.net.URLEncoder .
if (urlEncode != null) {
    try {
        return (String) urlEncode.invoke(s, new Object[] { s, enc });
    } catch (IllegalAccessException e) {
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof UnsupportedEncodingException) {
            throw (UnsupportedEncodingException) e.getTargetException();
        } else if (e.getTargetException() instanceof RuntimeException) {
...
Stringencode(String s, String encoding)
Calls java.net.URLEncoder.encode(String, String) via reflection, if we are running on JRE 1.4 or later, otherwise reverts to the deprecated URLEncoder.encode(String) method.
Class c = URLEncoder.class;
String result = null;
try {
    Method m = c.getDeclaredMethod("encode", STRING_ARGS_2);
    try {
        result = (String) m.invoke(null, new Object[] { s, encoding });
    } catch (InvocationTargetException e) {
        e.printStackTrace();
...
Stringencode(String source)
encode
StringBuffer sb = new StringBuffer();
encode(source, sb);
return sb.toString();
Stringencode(String src, String srcCode, String destCode)
encode
try {
    byte[] strby = src.getBytes(srcCode);
    String dest = new String(strby, destCode);
    return dest;
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
return null;
...
Stringencode(String str)
encodes the given string using URLEncoder.
try {
    return java.net.URLEncoder.encode(str, "UTF-8");
} catch (java.io.UnsupportedEncodingException ue) {
    return str;
Stringencode(String str)
Encode the specified string.
try {
    return URLEncoder.encode(str, encode);
} catch (UnsupportedEncodingException e) {
    return str;