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

StringescapeRegexp(final String str)
escape Regexp
if (str == null) {
    return null;
final StringBuffer buf = new StringBuffer();
final int len = str.length();
for (int i = 0; i < len; i++) {
    final char c = str.charAt(i);
    if (Arrays.binarySearch(REGEXP_CHARS, c) >= 0) {
...
StringescapeRegexpSymbol(String expr)
The method compiles specified string to convert special symbol of regular expression, it avoids the special char in string is parsed as regular symbol.
char[] specialSymbol = new char[] { '$', '(', ')', '*', '+', '.', '[', '?', '^', '{', '|', '}', '\\' };
List<Character> specialList = new ArrayList<Character>();
for (int i = 0; i < specialSymbol.length; i++) {
    specialList.add(Character.valueOf(specialSymbol[i]));
StringBuffer sb = new StringBuffer(expr);
for (int i = 0; i < sb.length(); i++) {
    int index = specialList.indexOf(Character.valueOf(sb.charAt(i)));
...
StringescapeReservedWord(String input)
escape Reserved Word
return RESERVED_KEYWORDS.contains(input) ? "\"" + input + "\"" : input;
StringescapeSelected(String str, String chars)
escape Selected
chars = "%" + chars;
for (int i = 0; i < chars.length(); i++) {
    String charAsStr = chars.substring(i, i + 1);
    try {
        str = str.replace(charAsStr, URLEncoder.encode(charAsStr, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("UTF-8 encoding not found:", e);
return str;
StringescapeSolr(String value)
escape Solr
StringBuilder stringBuilder = new StringBuilder(value);
for (int i = 0; i < stringBuilder.length(); i++) {
    boolean isCharacterToBeEscaped = Arrays.binarySearch(CHARACTERS_TO_BE_ESCAPED_FOR_SOLR,
            stringBuilder.charAt(i)) >= 0;
    if (isCharacterToBeEscaped) {
        stringBuilder.insert(i, '\\');
        i++;
return stringBuilder.toString();
StringescapeToFileName(String name)
Create escaped filename from a string
try {
    return java.net.URLEncoder.encode(name, "UTF-8");
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
StringescapeUnicode(String s)
escape Unicode
if (s == null) {
    return null;
StringBuilder result = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    char ca = (Character.isDefined(c)) ? c : UNICODE_REPLACEMENT;
    result.append(ca);
...
StringescapeUnicodeString(final String input, final boolean escapeAscii)
Escapes the unicode in the given string.
StringBuilder returnValue = new StringBuilder("");
for (int i = 0; i < input.length(); i++) {
    char ch = input.charAt(i);
    if (!escapeAscii && ch >= 0x0020 && ch <= 0x007e) {
        returnValue.append(ch);
    } else {
        returnValue.append("\\u");
        String hex = Integer.toHexString(input.charAt(i) & 0xFFFF);
...
StringescapeUnsafeCharacters(String anyURI, boolean escapePercent)
Escapes the "delim" and "unwise" characters as specified by rfc2396.
if (anyURI == null)
    return null;
if (escapePercent)
    anyURI = anyURI.replaceAll("\\" + PERCENT, (String) fCharToEscaped.get(PERCENT)); 
String key = null;
for (Iterator i = fCharToEscaped.keySet().iterator(); i.hasNext();) {
    key = (String) i.next();
    if (key.equals(PERCENT))
...
StringescapeWiki(String s)
escape Wiki
s = s.replaceAll(" ", "_");
s = s.replaceAll("\\t", "_");
try {
    s = URLEncoder.encode(s, "UTF8");
    return s;
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
return "";