Java Utililty Methods String Whitespace Clean

List of utility methods to do String Whitespace Clean

Description

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

Method

StringcleanWhitespace(String definition)
Helper method to make definition strings more Tab-delimited/Excel friendly
if (definition == null) {
    return "NULL";
return definition.replace("\r", "[CR]").replace("\n", "[LF]").replace("\t", "[TAB]").trim();
StringcleanWhitespace(String in)
Clean - Remove all white spaces
char[] inArray = in.toCharArray();
StringBuffer out = new StringBuffer(inArray.length);
boolean lastWasSpace = false;
for (int i = 0; i < inArray.length; i++) {
    char c = inArray[i];
    if (Character.isWhitespace(c)) {
        if (!lastWasSpace)
            out.append(' ');
...
StringcleanWhiteSpace(String s, boolean all)
clean White Space
if (s == null) {
    return null;
s = s.trim();
if (all) {
    s = s.replaceAll("\\s+", "");
return s;
...
StringcleanWhitespace(String str)
clean Whitespace
if (str != null) {
    return str.trim().replaceAll("\u00a0", "");
return null;
StringcleanWhitespace(String string)
Reduce multiple consecutive whitspaces to a single space (also removing the \n, \t etc.) and also trim the string
String reduced = string.replaceAll("\\s+", " ");
return reduced.trim();
StringcleanWhitespaceAndIndentation(String s, String indent)
clean Whitespace And Indentation
String res = s.replaceAll("[\r\n]", "\n") 
        .replaceAll("\n\\s*", "\n") 
        .replaceAll("\\s*\n", "\n") 
        .replaceAll("[\r\n]", "\n" + indent) 
        .replaceAll("\n\\s*\n", "\n") 
        .replaceAll("}\n", "}\n\n"); 
if (indent.length() >= 2) {
    res = res.replaceAll("\n\\s*}$", "\n" + indent.substring(2) + "}"); 
...
StringcleanWhitespaces(String string)
Replace whitespace characters with space character.
if (string != null) {
    char[] cc = string.toCharArray();
    for (int ci = cc.length - 1; ci > 0; ci--) {
        if (Character.isWhitespace(cc[ci])) {
            cc[ci] = ' ';
    return new String(cc);
...