Here you can find the source of encodeURI(String s)
encodeURIComponent
function.
Parameter | Description |
---|---|
s | The String to be encoded |
public static String encodeURI(String s)
//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; } }