Java Utililty Methods String Sanitize

List of utility methods to do String Sanitize

Description

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

Method

StringsanitizeForJson(String data)
Sanitize a string for inclusion in a JSON string Because most browsers will interpret HTML tags e.g.
data = data.replace("<", "\\x3C");
data = data.replace(">", "\\x3E");
data = data.replace("\u2028", "");
return data;
StringsanitizeForLogMessage(String unsanitizedString)
Sanitizes the log message, only allow br element and span element with class attribute equals to "bold" or "text-danger".
if (unsanitizedString == null) {
    return null;
return unsanitizedString.replaceAll("<(?!(/?(span( class=\"(bold|text-danger)\")?|br)>))", "&lt;")
        .replaceAll("(?<!(</?(span( class=\"(bold|text-danger)\")?|br)))>", "&gt;")
        .replaceAll("(?<!<span class=(\"(bold|text-danger))?)\"(?!>)", "&quot;")
        .replaceAll("(?<!<)/(?!(span|br)>)", "&#x2f;").replace("'", "&#39;")
        .replaceAll("&(?!(amp;)|(lt;)|(gt;)|(quot;)|(#x2f;)|(#39;))", "&amp;");
...
StringsanitizeForSearch(String str)
Sanitize the string for searching.
if (str == null) {
    return null;
return str
        .replace("`", " ").replace("!", " ").replace("#", " ").replace("$", " ").replace("%", " ")
        .replace("^", " ").replace("&", " ").replace("[", " ").replace("]", " ").replace("{", " ")
        .replace("}", " ").replace("|", " ").replace(";", " ").replace("*", " ").replace(".", " ")
        .replace("?", " ").replace("'", " ").replace("/", " ")
...
StringsanitizeForSemgrexName(String text)
Sanitizes the given string into a Semgrex friendly name
text = text.replaceAll("\\.", "_DOT_");
text = text.replaceAll("\\,", "_COMMA_");
text = text.replaceAll("\\\\", "_BSLASH_");
text = text.replaceAll("\\/", "_BSLASH_");
text = text.replaceAll("\\?", "_QUES_");
text = text.replaceAll("\\!", "_BANG_");
text = text.replaceAll("\\$", "_DOL_");
text = text.replaceAll("\\!", "_BANG_");
...
StringsanitizeForTableName(String input)
Strips illegal characters so that the given string can be used as a SQLite table name.
return input.replaceAll("[^A-Za-z0-9]", "");
StringsanitizeForUri(String uri, String replace)
Sanitizes a URI to conform with the URI standards RFC3986 http://www.ietf.org/rfc/rfc3986.txt.
uri = uri.replaceAll("[^a-zA-Z0-9\\._-]+", replace);
return uri;
StringsanitizeFullPrefixKey(String propKey)
sanitize Full Prefix Key
return propKey + STRIP_SUFFIX;
StringsanitizeGoogleId(String rawGoogleId)
Sanitizes a google ID by removing leading/trailing whitespace and the trailing "@gmail.com".
if (rawGoogleId == null) {
    return null;
String sanitized = rawGoogleId.trim();
if (sanitized.toLowerCase().endsWith("@gmail.com")) {
    sanitized = sanitized.split("@")[0];
return sanitized.trim();
...
StringsanitizeHeader(String header)
sanitize Header
if (header == null)
    return null;
final char[] chars = header.toCharArray();
for (int i = 0, j = chars.length; i < j; i++) {
    if (!isAsciiPrintable(chars[i])) {
        chars[i] = '.';
return new String(chars);
StringsanitizeID(String name)
sanitize ID
name = name.replaceAll("[^a-zA-Z0-9_\\-\\.]", "_");
if (name.matches("([0-9]|\\.|\\-).*"))
    name = "a" + name;
return name;