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

Stringescape(final @Nonnull String string)
escape
return URLEncoder.encode(string
        .replace("/", "\\/").replace(":", "\\:").replace("[", "\\[").replace("]", "\\]").replace("(", "\\(")
        .replace(")", "\\)"), "UTF-8");
Stringescape(final String s)
escape
try {
    return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
    return s;
Stringescape(final String value)
Escape a value when writing XML.
String escapedValue = value;
Iterator<String> iter = ESCAPES.keySet().iterator();
while (iter.hasNext()) {
    String raw = iter.next();
    String escaped = ESCAPES.get(raw);
    escapedValue = escapedValue.replace(raw, escaped);
return escapedValue;
...
Stringescape(String original)
Escape the given string for use in Trillian XML log format.
String escaped = new String(original);
try {
    escaped = URLEncoder.encode(escaped, encoding);
} catch (UnsupportedEncodingException exception) {
    throw new RuntimeException(exception);
return escaped.replace("+", "%20");
Stringescape(String s)
escape
StringBuilder builder = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (SPECIAL_CHARS.contains(c)) {
        builder.append(ESCAPED_SPECIAL_CHARS.get(SPECIAL_CHARS.indexOf(c)));
    } else {
        builder.append(c);
return builder.toString();
Stringescape(String s)
Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme.
try {
    return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
Stringescape(String str)

Escapes any values it finds into their String form.

So a tab becomes the characters '\\' and 't'.

int sz = str.length();
StringBuilder buffer = new StringBuilder(2 * sz);
for (int i = 0; i < sz; i++) {
    char ch = str.charAt(i);
    if (ch > 0xfff) {
        buffer.append("\\u" + Integer.toHexString(ch));
    } else if (ch > 0xff) {
        buffer.append("\\u0" + Integer.toHexString(ch));
...
Stringescape(String string, String chars)
escape
Map<String, String> replacements = new HashMap<String, String>();
for (int i = 0; i < chars.length(); i++) {
    String original = chars.substring(i, i + 1);
    replacements.put(original, escape(original));
StringBuffer escapedStringBuilder = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
    String original = string.substring(i, i + 1);
...
StringescapeASN1(String str)
Escapes Strings to be used in ASN.1 attributes (like LDAP attributes)
StringBuffer out = new StringBuffer();
char c;
boolean escape;
for (int i = 0; i < str.length(); i++) {
    c = str.charAt(i);
    escape = false;
    if (i == 0 && ASN1_FIRSTCHAR_ESCAPE_CHARS.indexOf(c) != -1) {
        escape = true;
...
StringescapeCellText(String text, boolean wrap, boolean multiline)
Escape character that has special meaning in HTML such as <, &, etc..
final StringBuffer out = new StringBuffer();
for (int j = 0, tl = text.length(); j < tl; ++j) {
    char cc = text.charAt(j);
    switch (cc) {
    case '&':
        out.append("&amp;");
        break;
    case '<':
...