Java String Whitespace Normalize normaliseWhitespace(String string)

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

Description

Normalise the whitespace within this string; multiple spaces collapse to a single, and all whitespace characters (e.g.

License

Apache License

Parameter

Parameter Description
string content to normalise

Return

normalised string

Declaration

public static String normaliseWhitespace(String string) 

Method Source Code

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

public class Main {
    /**//www  .  j  ava2 s. c o m
     * Normalise the whitespace within this string; multiple spaces collapse to a single, and all whitespace characters
     * (e.g. newline, tab) convert to a simple space
     * @param string content to normalise
     * @return normalised string
     */
    public static String normaliseWhitespace(String string) {
        StringBuilder sb = new StringBuilder(string.length());
        appendNormalisedWhitespace(sb, string, false);
        return sb.toString();
    }

    /**
     * After normalizing the whitespace within a string, appends it to a string builder.
     * @param accum builder to append to
     * @param string string to normalize whitespace within
     * @param stripLeading set to true if you wish to remove any leading whitespace
     */
    public static void appendNormalisedWhitespace(StringBuilder accum, String string, boolean stripLeading) {
        boolean lastWasWhite = false;
        boolean reachedNonWhite = false;

        int len = string.length();
        int c;
        for (int i = 0; i < len; i += Character.charCount(c)) {
            c = string.codePointAt(i);
            if (isWhitespace(c)) {
                if ((stripLeading && !reachedNonWhite) || lastWasWhite)
                    continue;
                accum.append(' ');
                lastWasWhite = true;
            } else {
                accum.appendCodePoint(c);
                lastWasWhite = false;
                reachedNonWhite = true;
            }
        }
    }

    /**
     * Tests if a code point is "whitespace" as defined in the HTML spec.
     * @param c code point to test
     * @return true if code point is whitespace, false otherwise
     */
    public static boolean isWhitespace(int c) {
        return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r';
    }
}

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)
  6. normalizeWhiteSpace(String input)