Java String Escape percentEncode(String s)

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

Description

Percent encode a string

License

Open Source License

Parameter

Parameter Description
s a string to encode

Return

an encoded string

Declaration

public static String percentEncode(String s) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

public class Main {
    public static final String ENCODING = "UTF-8";

    /**//  w w w  . j  a  v a  2  s.c o  m
     * Percent encode a string
     * @param s a string to encode
     * @return an encoded string
     */
    public static String percentEncode(String s) {
        String result = "";
        try {
            if (s != null) {
                result = URLEncoder.encode(s, ENCODING).replace("%7E", "~")
                        .replace("*", "%2A").replace("+", "%20");
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return result;
    }
}

Related

  1. escapeWiki(String s)
  2. escapeXML(String message)
  3. escapeXml(String str)
  4. escapeXML(String str)
  5. percentEncode(String s)
  6. percentEncode(String value)
  7. percentEncode(String value, String encoding)
  8. percentEncodeRfc3986(final String string)