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(String methodHeader)
sanitize
methodHeader = methodHeader.toLowerCase().trim();
if (methodHeader.startsWith("is")) {
    return methodHeader.substring(2);
if (methodHeader.startsWith("get")) {
    return methodHeader.substring(3);
return methodHeader;
...
Stringsanitize(String mimeType)
sanitize
for (String accepted : new String[] { "application/json", "text/plain" }) {
    if (accepted.equalsIgnoreCase(mimeType)) {
        return accepted;
return "text/plain";
Stringsanitize(String original)
sanitize
String cleaned = original.replace("\r", "|").replace("\n", "|").replace("\t", "|");
if (!cleaned.equals(original)) {
    cleaned += SANITIZED_FLAG;
return cleaned;
Stringsanitize(String path)
Sanitizes path by ensuring that path starts with a slash and doesn't have one at the end
return withLeadingSlash(withoutSlashAtEnd(path));
Stringsanitize(String s)
Sanitizes a string so it can be used as an identifier
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); ++i) {
    char c = s.charAt(i);
    switch (c) {
    case '\u00c0':
    case '\u00c1':
    case '\u00c3':
    case '\u00c4':
...
Stringsanitize(String s)
sanitize
StringBuilder sb = null;
final int limit = s.length();
for (int i = 0; i < limit; i++) {
    char c = s.charAt(i);
    switch (c) {
    case '<':
        if (sb == null) {
            sb = new StringBuilder();
...
Stringsanitize(String s)
Ensures that s is friendly for a URL or file system.
return s.replace(':', '-').replace('_', '-').replace('.', '-').replace('/', '-').replace('\\', '-');
Stringsanitize(String s, boolean allowColorCodes)
sanitize
while (s.endsWith("\u00a7"))
    s.substring(0, s.length() - 1);
if (!allowColorCodes)
    s = s.replace('\u00a7', ' ').replace((char) 0x03, ' ').replace((char) 0x02, ' ')
            .replace((char) 0x1D, ' ').replace((char) 0x1F, ' ').replace((char) 0x16, ' ');
s = s.replace('\n', ' ').replace('\r', ' ').replace('\b', ' ').replace('\f', ' ').replace("\t", "    ");
return s;
Stringsanitize(String source)
sanitize
StringBuilder result = new StringBuilder();
int last = source.length();
for (int i = 0; i < last; i++) {
    char ch = source.charAt(i);
    if (ch >= 'a' && ch <= 'z') {
        result.append(ch);
    } else if (ch >= '0' && ch <= '9') {
        result.append(ch);
...
Stringsanitize(String str)
sanitize
if (str.length() > MAX_LENGTH) {
    str = str.substring(0, MAX_LENGTH);
if (isPrintableString(str)) {
    return str;
StringBuilder builder = new StringBuilder(str.length());
for (int i = 0; i < str.length(); i++) {
...