Here you can find the source of URLEncode(String in)
public static String URLEncode(String in)
//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(); } }