Java Utililty Methods URI Encode

List of utility methods to do URI Encode

Description

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

Method

StringencodeUri(String uri)
encode Uri
int quoIndex = uri.indexOf("?");
if (quoIndex < 0 || quoIndex == uri.length()) {
    return uri;
String url = uri.substring(0, quoIndex);
String param = uri.substring(quoIndex + 1);
String encodedUri = uri;
try {
...
StringencodeUri(URI uri)
Encodes a URI into a flat character string.
if (uri == null) {
    return ""; 
return uri.toASCIIString();
StringencodeURI(URI uri)
encode URI
return encodeURI(uri.toString());
StringencodeURIComponent(final String s)
encode URI Component
if (s == null || s.length() == 0) {
    return s;
try {
    return URLEncoder.encode(s, "utf-8");
} catch (UnsupportedEncodingException ex) {
    throw new RuntimeException(ex);
StringencodeURIComponent(String component)
encode URI Component
String result;
try {
    result = URLEncoder.encode(component, "UTF-8").replaceAll("%28", "(").replaceAll("%29", ")")
            .replaceAll("\\+", "%20").replaceAll("%27", "'").replaceAll("%21", "!").replaceAll("%7E", "~");
} catch (java.io.UnsupportedEncodingException e) {
    result = component;
return result;
...
StringencodeURIComponent(String input)
encode URI Component
try {
    return URLEncoder.encode(input, "UTF-8").replaceAll(" ", "%20").replaceAll("!", "%21")
            .replaceAll("'", "%27").replaceAll("\\(", "%28").replaceAll("\\)", "%29")
            .replaceAll("\\+", "%2B").replaceAll("\\:", "%3A").replaceAll("~", "%7E");
} catch (UnsupportedEncodingException e) {
return null;
StringencodeURIComponent(String s, String charset)
Encodes the passed String as UTF-8 using an algorithm that's compatible with JavaScript's encodeURIComponent function.
if (s == null) {
    return null;
} else {
    String result;
    try {
        result = URLEncoder.encode(s, charset).replaceAll("\\+", "%20").replaceAll("\\%21", "!")
                .replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")")
                .replaceAll("\\%7E", "~");
...
StringencodeURIComponent(String uriComp)
encode URI Component
try {
    return URLEncoder.encode(uriComp, "UTF-8").replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException("Encoding URI component \"" + uriComp + "\" failed", e);
StringencodeUriComponent(String value)
encode Uri Component
try {
    return convertToGAStyleEncoding(URLEncoder.encode(value, "UTF-8"));
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
StringencodeURIComponentForJavaScript(String str)
URI encodes a string for Javascript consumption; client should call decodeURIComponent to decode
return URLEncoder.encode(str, "UTF-8").replaceAll("\\+", "%20").replaceAll("\\%21", "!")
        .replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")").replaceAll("\\%7E", "~")
        .replaceAll("\\%3B", ";");