Java Utililty Methods String Whitespace Normalize

List of utility methods to do String Whitespace Normalize

Description

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

Method

StringnormalizeWhitespace(String source)
INTERNAL: Replaces sequences of one or more ' ', \t, \n, \r by a single space, returning the new string.
char[] string = source.toCharArray();
int pos = 0;
boolean previousWasWS = false;
for (int ix = 0; ix < string.length; ix++) {
    switch (string[ix]) {
    case ' ':
    case '\t':
    case '\n':
...
StringnormalizeWhiteSpace(String src)
Replace all blocks of white space by a single space character, just used for creating test cases.
StringBuilder result = new StringBuilder(src.length());
boolean inWhitespaceBlock = false;
for (int i = 0; i < src.length(); i++) {
    char c = src.charAt(i);
    if (Character.isWhitespace(c)) {
        if (!inWhitespaceBlock) {
            result.append(" ");
            inWhitespaceBlock = true;
...
StringnormalizeWhitespace(String text)
Translates multiple whitespace into single space character.
text = text.replaceAll("(\r\n|\r)", "\n");
text = text.replaceAll("(?:(?![\n])\\s+)", " "); 
text = text.replaceAll("<br>", ""); 
return text;
StringnormalizeWhitespaces(String s)
Replaces all duplicated whitespace characters with single space.
int length = s.length();
StringBuffer normalized = new StringBuffer(length);
boolean needSpace = false;
for (int index = 0; index < length; index++) {
    char c = s.charAt(index);
    if (Character.isWhitespace(c)) {
        needSpace = true;
    } else {
...
StringnormalizeWhitespaces(String text)
normalize Whitespaces
return compressWhitespaces(text).trim();