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

Stringsanitize(char orig)
sanitize
return sanitize(String.valueOf(orig));
Stringsanitize(CharSequence string)
Clean strings from illegal XML 1.0 characters.
StringBuilder sb = new StringBuilder();
for (int i = 0, len = string.length(); i < len; i++) {
    char c = string.charAt(i);
    boolean legal = c == '\u0009' || c == '\n' || c == '\r' || (c >= '\u0020' && c <= '\uD7FF')
            || (c >= '\uE000' && c <= '\uFFFD');
    if (legal) {
        sb.append(c);
return sb.toString();
voidsanitize(double[] outdata)
Sanitizes a double array, meaning - replace NaN by zero - replace -infinity by zero - replace infinity by zero
for (int i = 0; i < outdata.length; i++) {
    outdata[i] = outdata[i] == Double.NaN ? 0.0 : outdata[i];
    outdata[i] = outdata[i] == Double.NEGATIVE_INFINITY ? (0.0) : outdata[i];
    outdata[i] = outdata[i] == Double.POSITIVE_INFINITY ? (0.0) : outdata[i];
Stringsanitize(final String main)
Removes all chars not accepted by the file system naming scheme.
return main.replace("<", "").replace(">", "").replace(":", "").replace("/", "").replace("\\", "")
        .replace("|", "").replace("?", "").replace("*", "").replace("\"", "").replace("\r", "")
        .replace("\n", "");
Stringsanitize(final String name)
Sanitize bucket and folder names according to AWS guidelines.
String retval = name;
retval = retval.replace('/', '-');
retval = retval.replace('\\', '-');
return retval;
Stringsanitize(final String s)
Removes unprintable characters from the given string.
if (s == null)
    return null;
StringBuffer buf = new StringBuffer(s);
for (int i = 0; i < buf.length(); i++) {
    final char c = buf.charAt(i);
    if (c != '\t' && c != '\n' && (c < ' ' || c > '~')) {
        buf = buf.deleteCharAt(i--);
return buf.toString();
intsanitize(final String singleOctets, byte[] dest)
Transforms the given string into the given destination byte array truncating each character into a byte and skipping carriage returns and line feeds if any.
final int capacity = dest.length;
final char[] src = singleOctets.toCharArray();
int limit = 0;
for (int i = 0; i < capacity; i++) {
    final char c = src[i];
    if (c == '\r' || c == '\n' || c == ' ')
        continue;
    if (c > Byte.MAX_VALUE)
...
Stringsanitize(String input)
Remove all characters except alphanumerics and space.
return input.replaceAll("[^A-Za-z0-9 ]", " ");
Stringsanitize(String input)
This function will remove all chars from the string that confuse dotty Currently '<' and '>' Expand this if you run into trouble with other chars
String output = "";
for (char c : input.toCharArray()) {
    if (!(c == '<' || c == '>')) {
        output += c;
    } else {
        output += ' ';
return output;
Stringsanitize(String input, String prohibitedStringsRegexp)
sanitize
return sanitize(input, prohibitedStringsRegexp, REPLACEMENT_STRING);