Java String Escape percentEncodeRfc3986(final String string)

Here you can find the source of percentEncodeRfc3986(final String string)

Description

Percent-encode values according the RFC 3986.

License

Apache License

Parameter

Parameter Description
string Decoded string.

Return

Encoded string per RFC 3986.

Declaration

public static String percentEncodeRfc3986(final String string) 

Method Source Code


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

import java.io.*;
import java.net.*;

public class Main {
    /**/* w w  w  .j a v a2s. c om*/
     * Percent-encode values according the RFC 3986. The built-in Java URLEncoder does not encode
     * according to the RFC, so we make the extra replacements.
     *
     * @param string Decoded string.
     * @return Encoded string per RFC 3986.
     */
    public static String percentEncodeRfc3986(final String string) {
        try {
            return URLEncoder.encode(string, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
        } catch (final UnsupportedEncodingException e) {
            return string;
        }
    }
}

Related

  1. escapeXML(String str)
  2. percentEncode(String s)
  3. percentEncode(String s)
  4. percentEncode(String value)
  5. percentEncode(String value, String encoding)