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

StringsanitizeString(final String s)
sanitize String
if (s == null) {
    return "";
} else {
    return s.trim();
StringsanitizeString(String dirtyString)
sanitize String
StringBuilder sb = new StringBuilder(dirtyString.length());
for (int i = 0; i < dirtyString.length(); i++) {
    if (!Character.isIdentifierIgnorable(dirtyString.charAt(i))) {
        sb.append(dirtyString.charAt(i));
return sb.toString();
StringsanitizeString(String input)
sanitize String
if (input != null) {
    char[] docCharArray = input.toCharArray();
    replaceInvalidXmlChars(docCharArray);
    return String.valueOf(docCharArray);
return input;
StringsanitizeString(String s)
Removes newlines and tabs from a string
String n = null;
if (s != null) {
    n = s.replaceAll("\t", "");
    n = n.replaceAll("\n", "");
return n;
StringsanitizeString(String s)
Clean any XML from the string
return s.replace("<", "").replace(">", "").replace("/>", "");
StringsanitizeString(String s)
sanitize String
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
    if (c < 0x20) {
        sb.append(" ");
    } else {
        sb.append(c);
return sb.toString().trim();
StringsanitizeString(String str)
Escapes all escapable characters in a string.
String newStr = "";
boolean escape = false;
for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    if (escape) {
        switch (c) {
        case '\\':
            newStr += "\\";
...
StringsanitizeStringAsLiteral(String literal)
Sanitize a String for use in a SQL statement
if (literal == null) {
    return "NULL";
String sanitizedLiteral = literal.replace("'", "''");
int nullIndex = sanitizedLiteral.indexOf('\0');
if (nullIndex >= 0) {
    StringBuilder builder = new StringBuilder();
    int start = 0;
...
StringsanitizeStringForXPath(String text)
Convert the string to a safer version for XPath For example: Will o' The Wisp => concat('Will o' , "'" , ' The Wisp' , '') This will result in the same string when read by XPath.
StringBuilder result = new StringBuilder();
int startOfChain = 0;
int textLength = text.length();
boolean isSingleQuotationChain = false;
for (int currentPos = 0; currentPos <= textLength; currentPos++) {
    boolean isChainBroken = currentPos >= textLength
            || isSingleQuotationChain && text.charAt(currentPos) != '\''
            || !isSingleQuotationChain && text.charAt(currentPos) == '\'';
...
StringsanitizeStringLiteral(String inputString)
Removes first and last character, assuming they're simple (') or double quotes (")
if (inputString != null && inputString != "") {
    if (inputString.charAt(0) == '"' && inputString.charAt(inputString.length() - 1) == '"'
            || inputString.charAt(0) == '\'' && inputString.charAt(inputString.length() - 1) == '\'') {
        if (inputString.length() > 2)
            inputString = inputString.substring(1, inputString.length() - 1);
        else
            inputString = "";
return inputString;