Java URI Encode encodeURI(String s)

Here you can find the source of encodeURI(String s)

Description

Encodes the passed String as UTF-8 using an algorithm that's compatible with JavaScript's encodeURIComponent function.

License

LGPL

Parameter

Parameter Description
s The String to be encoded

Return

the encoded String

Declaration

public static String encodeURI(String s) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.net.URLEncoder;

public class Main {
    /**/*from w w  w.  j  a  v  a 2  s  .c o m*/
     * Encodes the passed String as UTF-8 using an algorithm that's compatible
     * with JavaScript's
     * <code>encodeURIComponent</code> function. Returns
     * <code>null</code> if the String is
     * <code>null</code>.
     *
     * @param s The String to be encoded
     * @return the encoded String
     */
    public static String encodeURI(String s) {
        String result;
        try {
            result = URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20").replaceAll("\\%21", "!")
                    .replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")")
                    .replaceAll("\\%7E", "~");
        } // This exception should never occur.
        catch (Exception e) {
            result = s;
        }

        return result;
    }
}

Related

  1. encode(String uri)
  2. encodeForURI(String uriPart)
  3. encodeIfNeeded(String uri)
  4. encodeUri(final String uri)
  5. encodeUri(final URI uri)
  6. encodeUri(String string)
  7. encodeURI(String uri)
  8. encodeUri(String uri)
  9. encodeUri(String uri)