Java URL Encode URLEncode(String in)

Here you can find the source of URLEncode(String in)

Description

Encodes a string as follows: - The ASCII characters 'a' through 'z', 'A' through 'Z', '0' through '9', and ".", "-", "*", "_" remain the same.

License

Open Source License

Declaration

public static String URLEncode(String in) 

Method Source Code

//package com.java2s;

public class Main {
    private static final char[] hc = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };

    /**/*from   ww  w .  ja  v  a  2 s .c  o  m*/
     * Encodes a string as follows:
     * - The ASCII characters 'a' through 'z', 'A' through 'Z', '0' through
     *   '9', and ".", "-", "*", "_" remain the same.
     * - The space character ' ' is converted into a plus sign '+'.
     * - All other characters are converted into the 3-character string "%xy",
     *   where xy is the two-digit hexadecimal representation of the lower
     *   8-bits of the character.
     */
    public static String URLEncode(String in) {
        char[] chars = in.toCharArray();
        char c = ' ';
        StringBuffer out = new StringBuffer();

        for (int i = 0; i < chars.length; i++) {
            c = chars[i];
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
                out.append(c);
            } else if (c == ' ') {
                out.append('+');
            } else {
                out.append('%');
                out.append(hc[(c >> 4) & 0x0F]);
                out.append(hc[c & 0x0F]);
            }
        }

        return out.toString();
    }
}

Related

  1. uriDecode(String uri)
  2. uriEncode(String uriRef)
  3. URLEncode(byte[] input)
  4. urlEncode(byte[] rs)
  5. urlencode(byte[] unencodedBytes)
  6. urlEncode(String origString)
  7. URLencode(String s)
  8. urlEncode(String str)
  9. urlEncode(String str)