Java URL Encode URLencode(String s)

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

Description

URL-encodes the given string

License

Open Source License

Parameter

Parameter Description
s a parameter

Declaration

public static String URLencode(String s) 

Method Source Code

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

public class Main {
    /**//  w  w w. j  a v a  2 s .  co m
     * URL-encodes the given string
     * @param s
     * @return
     */
    public static String URLencode(String s) {
        if (s != null) {
            StringBuffer tmp = new StringBuffer();
            int i = 0;
            try {
                while (true) {
                    int b = (int) s.charAt(i++);
                    if ((b >= 0x30 && b <= 0x39) || (b >= 0x41 && b <= 0x5A) || (b >= 0x61 && b <= 0x7A)) {
                        tmp.append((char) b);
                    } else {
                        tmp.append("%");
                        if (b <= 0xf)
                            tmp.append("0");
                        tmp.append(Integer.toHexString(b));
                    }
                }
            } catch (Exception e) {
            }
            return tmp.toString();
        }
        return null;
    }
}

Related

  1. URLEncode(byte[] input)
  2. urlEncode(byte[] rs)
  3. urlencode(byte[] unencodedBytes)
  4. URLEncode(String in)
  5. urlEncode(String origString)
  6. urlEncode(String str)
  7. urlEncode(String str)
  8. urlEncode(String str)
  9. urlEncode(String urlPlain)