Java URL Encode encode(String value)

Here you can find the source of encode(String value)

Description

encode

License

Apache License

Declaration

public static String encode(String value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

public class Main {
    public static String encode(String value) {
        String encoded = null;//from ww w  . ja  v a2  s.co m
        try {
            encoded = URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException ignore) {
        }

        StringBuilder buf = new StringBuilder(encoded.length());
        char focus;
        for (int i = 0; i < encoded.length(); i++) {
            focus = encoded.charAt(i);
            if (focus == '*')
                buf.append("%2A");
            else if (focus == '+')
                buf.append("%20");
            else if (focus == '%' && (i + 1) < encoded.length() && encoded.charAt(i + 1) == '7'
                    && encoded.charAt(i + 2) == 'E') {
                buf.append('~');
                i += 2;
            } else
                buf.append(focus);
        }

        return buf.toString();
    }
}

Related

  1. encode(String text)
  2. encode(String toBeEncoded, String charSet, String defaultReturnValue)
  3. encode(String value)
  4. encode(String value)
  5. encode(String value)
  6. encode(String value, String charset)
  7. encode(String value, String encoding)
  8. encodeArray(final String[] val)
  9. encodeForUrl(final String s)