Java URL Encode URLEncoder(String str)

Here you can find the source of URLEncoder(String str)

Description

URL Encoder

License

Open Source License

Declaration

public static String URLEncoder(String str) 

Method Source Code

//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();
    }
}

Related

  1. URLEncodeFilePath(String s)
  2. urlEncodeForSpaces(String href)
  3. urlEncodeForSpaces(String input)
  4. urlEncodeForSpaces(String input)
  5. urlEncodeParameter(String value)
  6. urlEncodeSpace(String s)
  7. urlParameterEncode(String s)
  8. urlPercentEncodeTwo(char c)