Java Utililty Methods String Escape

List of utility methods to do String Escape

Description

The list of methods to do String Escape are organized into topic(s).

Method

StringescapeXML(String message)
Formats the message to be XML compatible, with the XML escaping.
StringBuilder msgBuffer = new StringBuilder(message.length() * 2);
escapeXML(message, msgBuffer);
return msgBuffer.toString();
StringescapeXml(String str)

Escapes XML entities in a String.

if (str == null) {
    return str;
StringBuffer buf = new StringBuffer(str.length() * 2);
int i;
for (i = 0; i < str.length(); ++i) {
    char ch = str.charAt(i);
    String entityName = getEntityName(ch);
...
StringescapeXML(String str)
escape some special xml chars.
if (str == null)
    return null;
str = str.replace("&", "&amp;");
str = str.replace("'", "&apos;");
str = str.replace("\"", "&quot;");
str = str.replace("<", "&lt;");
str = str.replace(">", "&gt;");
StringBuilder sb = new StringBuilder();
...
StringpercentEncode(String s)
percent Encode
if (s == null) {
    return "";
try {
    return URLEncoder.encode(s, ENCODING)
            .replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
} catch (UnsupportedEncodingException uee) {
    throw new RuntimeException(uee.getMessage(), uee);
...
StringpercentEncode(String s)
Percent encode a string
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;
StringpercentEncode(String value)
percent Encode
return value == null ? EMPTY : URLEncoder.encode(value, encode);
StringpercentEncode(String value, String encoding)
percent Encode
return value != null
        ? URLEncoder.encode(value, encoding).replace("+", "%20").replace("*", "%2A").replace("%7E", "~")
        : null;
StringpercentEncodeRfc3986(final String string)
Percent-encode values according the RFC 3986.
try {
    return URLEncoder.encode(string, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
} catch (final UnsupportedEncodingException e) {
    return string;