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

StringescapeCharacters(String string)
Substitutes all illegal characters in the given string by the value of EscapingXMLStreamWriter#substitute .
char[] copy = null;
boolean copied = false;
for (int i = 0; i < string.length(); i++) {
    if (isIllegal(string.charAt(i))) {
        if (!copied) {
            copy = string.toCharArray();
            copied = true;
        copy[i] = substitute;
return copied ? new String(copy) : string;
StringescapeHTML(String s)
escape HTML
StringBuilder buffer = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    if (c != '\r') {
        final String escaped = HTML_CHAR_ESCAPED_MAP.get(c);
        buffer.append(escaped != null ? escaped : c);
return buffer.toString();
StringescapeHTML(String source)
Convert a regular string to a string with escaped HTML entities (e.g.
StringBuilder ret = new StringBuilder();
String val = null;
int idx = 0;
for (idx = 0; idx < source.length(); idx++) {
    val = s_reverseHtmlEntities.get(source.substring(idx, idx + 1));
    if (val == null) {
        ret.append(source.charAt(idx));
    } else {
...
StringescapeHTMLTags(String in)
This method takes a string which may contain HTML tags (ie, <b>, <table>, etc) and converts the '<'' and '>' characters to their HTML escape sequences.
if (in == null) {
    return null;
char ch;
int i = 0;
int last = 0;
char[] input = in.toCharArray();
int len = input.length;
...
StringescapeInstanceIdentifier(String instanceIdentifier)
Formats the instance identifier to a form suitable for directory names.
try {
    return URLEncoder.encode(instanceIdentifier, "UTF-8");
} catch (UnsupportedEncodingException e) {
    return null;
StringescapeJavaString(String str)
escape Java String
StringBuilder builder = new StringBuilder(str.length());
for (int i = 0; i < str.length(); i++) {
    char ch = str.charAt(i);
    if (ch > 0xfff) {
        builder.append("\\u" + Integer.toHexString(ch).toUpperCase(Locale.ENGLISH));
    } else if (ch > 0xff) {
        builder.append("\\u0" + Integer.toHexString(ch).toUpperCase(Locale.ENGLISH));
    } else if (ch > 0x7f) {
...
voidescapeJavaStyleString(StringBuilder out, String str, boolean escapeSingleQuote, boolean escapeForwardSlash)
escape Java Style String
if (out == null) {
    throw new IllegalArgumentException("The Writer must not be null");
if (str == null) {
    return;
int sz = str.length();
for (int i = 0; i < sz; i++) {
...
StringescapeQuotes(String string)
Replaces in source string all the chars: ' , " or \ with escaped sequence, accordingly: \' , \" or \\
if (isHollow(string))
    return string;
return string.replaceAll("(['\"\\\\])", "\\\\$1");
StringescapeRegex(String regex)
escape Regex
StringBuilder newString = new StringBuilder();
char[] chars = regex.toCharArray();
for (char c : chars) {
    if (regexSpecialChars.contains(Character.valueOf(c))) {
        newString.append("\\");
    newString.append(c);
return newString.toString();
StringescapeRegexChars(String str, char... ignores)
escape Regex Chars
final char ESCAPE_CHAR = '\\';
final char[] REGEX_CHARS = { '.', ',', '?', '+', '-', '*', '^', '$', '|', '&', '{', '}', '[', ']', '(', ')',
        '\\' };
char[] regex_chars = REGEX_CHARS;
if (ignores.length > 0) {
    Set<Character> cs = new HashSet<Character>(REGEX_CHARS.length);
    for (int i = 0; i < REGEX_CHARS.length; i++)
        cs.add(REGEX_CHARS[i]);
...