Java String Whitespace Normalize normaliseWhitespace(String string)

Here you can find the source of normaliseWhitespace(String string)

Description

normalise Whitespace

License

LGPL

Declaration

public static String normaliseWhitespace(String string) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static String normaliseWhitespace(String string) {
        StringBuilder sb = new StringBuilder(string.length());

        boolean lastWasWhite = false;
        boolean modified = false;

        int l = string.length();
        for (int i = 0; i < l; i++) {
            int c = string.codePointAt(i);
            if (Character.isWhitespace(c)) {
                if (lastWasWhite) {
                    modified = true;/*w  w w. ja  v  a2  s .c  o  m*/
                    continue;
                }
                if (c != ' ')
                    modified = true;
                sb.append(' ');
                lastWasWhite = true;
            } else {
                sb.appendCodePoint(c);
                lastWasWhite = false;
            }
        }
        return modified ? sb.toString() : string;
    }
}

Related

  1. normaliseWhitespace(String string)
  2. normaliseWhitespace(String string)
  3. normalizeIndentationFromWhitespace(String str, int tabSize, boolean insertSpaces)
  4. normalizeWhitespace(final String str)
  5. normalizeWhitespace(String dirtyString)