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

StringsanitizeIdentifier(String identifier)
Sanitizes a stream identifier

Stream identifiers are used to identify the content of a specific stream.

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4 && i < identifier.length(); i++) {
    Character c = identifier.charAt(i);
    if (c.charValue() > 127 || !Character.isLetterOrDigit(c))
        continue;
    sb.append(c);
return String.format("%-4s", sb.toString());
...
StringsanitizeIdentifierName(String input)
Escapes illegal characters in given identifier.
if (input == null || input.isEmpty()) {
    return input;
final StringBuilder result = new StringBuilder();
char ch = input.charAt(0);
if (Character.isJavaIdentifierStart(ch)) {
    result.append(ch);
} else {
...
StringsanitizeIDs(String str)
Replace last "|" in pageIDs
if (str != null && str.length() > 0 && str.charAt(str.length() - 1) == '|') {
    str = str.substring(0, str.length() - 1);
return str;
StringsanitizeInput(String input)
"Sanitizes" the given input by replacing any new line characters with spaces.
return sanitizeInput(input, "\r\n", ' ', 255);
StringsanitizeInput(String string)
Sanitize HTML
return string.replaceAll("(?i)<script.*?>.*?</script.*?>", "") 
        .replaceAll("(?i)<script.*?/>", "") 
        .replaceAll("(?i)<script.*?>", "") 
        .replaceAll("(?i)<.*?javascript:.*?>.*?</.*?>", "") 
        .replaceAll("(?i)<.*?javascript:.*?/>", "") 
        .replaceAll("(?i)<.*?javascript:.*?>", "") 
        .replaceAll("(?i)<.*?\\s+on.*?>.*?</.*?>", "") 
        .replaceAll("(?i)<.*?\\s+on.*?/>", "") 
...
StringsanitizeJavascript(String originalString)
sanitize Javascript
return originalString.replaceAll("[\\\\(\"\')\\]\\[]", "\\\\$0");
StringsanitizeLine(String line)

Sanitize a single line of the "meta data".

final StringBuilder result = new StringBuilder();
final String[] lineRecord = line.split(CSV_SEPARATOR);
try {
    Integer.valueOf(lineRecord[0]);
    result.append(lineRecord[0]);
} catch (NumberFormatException e) {
    result.append(-1);
result.append(CSV_SEPARATOR);
result.append(lineRecord[1]);
result.append(CSV_SEPARATOR);
try {
    Integer.valueOf(lineRecord[2]);
    result.append(lineRecord[2]);
} catch (NumberFormatException e) {
    result.append(-1);
return result.toString();
StringsanitizeLineSpaces(String line)
Replaces all tabs and multiple spaces with a single space
return line.replaceAll("\\s+", " ").trim();
StringsanitizeLoggerNamePart(String name)
sanitize Logger Name Part
return name.replace('.', '_');
StringsanitizeMethodName(String methodName)
Sanitizes a potential method name so it is both valid and follows Java conventions (camel-cased, no underscores, etc.).
assert (methodName != null && methodName.length() > 0);
StringBuilder sb = new StringBuilder();
char firstChar = methodName.charAt(0);
if (Character.isJavaIdentifierStart(firstChar)) {
    if (Character.isUpperCase(firstChar)) {
        firstChar = Character.toLowerCase(firstChar);
    sb.append(firstChar);
...