Here you can find the source of URLEncoder(String str)
public static String URLEncoder(String str)
//package com.java2s; /**/* w w w. j a va 2 s. c o m*/ * Copyright (c) 2009-2010, Yahoo! Inc. All rights reserved. * Code licensed under the BSD License: * http://searchmarketing.yahoo.com/developer/docs/license.txt */ public class Main { public static String URLEncoder(String str) { if (str == null) { return null; } StringBuffer resultStr = new StringBuffer(str.length()); char tmpChar; for (int ix = 0; ix < str.length(); ix++) { tmpChar = str.charAt(ix); switch (tmpChar) { case ' ': resultStr.append("%20"); break; case '-': resultStr.append("%2D"); break; case '/': resultStr.append("%2F"); break; case ':': resultStr.append("%3A"); break; case '=': resultStr.append("%3D"); break; case '?': resultStr.append("%3F"); break; case '#': resultStr.append("%23"); break; case '\r': resultStr.append("%0D"); break; case '\n': resultStr.append("%0A"); break; default: resultStr.append(tmpChar); break; } } return resultStr.toString(); } }