Java URL Encode urlParameterEncode(String s)

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

Description

like urlEncode, but adds the equal sign

License

Apache License

Parameter

Parameter Description
s the url parameter to encode

Return

the encoded parameter

Declaration

public static String urlParameterEncode(String s) 

Method Source Code

//package com.java2s;
/*//from  w  w  w .  j a  v a 2s .c om
   Copyright 2006 OCLC Online Computer Library Center, Inc.
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

public class Main {
    /**
     * like urlEncode, but adds the equal sign
     * @param s the url parameter to encode
     * @return the encoded parameter
     */
    public static String urlParameterEncode(String s) {
        boolean changed = false;
        char c;
        StringBuffer sb = null;
        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);
            if (c == ' ' || c == '+' || c == '<' || c == '&' || c == '>' || c == '"' || c == '\'' || c == '='
                    || c > 0x7f) {
                if (!changed) {
                    if (i > 0)
                        sb = new StringBuffer(s.substring(0, i));
                    else
                        sb = new StringBuffer();
                    changed = true;
                }
                sb.append('%').append(Integer.toHexString(c));
            } else if (changed)
                sb.append(c);
        }
        if (!changed)
            return s;
        return sb.toString();
    }
}

Related

  1. urlEncodeForSpaces(String input)
  2. urlEncodeForSpaces(String input)
  3. urlEncodeParameter(String value)
  4. URLEncoder(String str)
  5. urlEncodeSpace(String s)
  6. urlPercentEncodeTwo(char c)