Android StringBuilder Append appendNormalisedWhitespace(StringBuilder accum, String string, boolean stripLeading)

Here you can find the source of appendNormalisedWhitespace(StringBuilder accum, String string, boolean stripLeading)

Description

After normalizing the whitespace within a string, appends it to a string builder.

Parameter

Parameter Description
accum builder to append to
string string to normalize whitespace within
stripLeading set to true if you wish to remove any leading whitespace

Declaration

public static void appendNormalisedWhitespace(StringBuilder accum,
        String string, boolean stripLeading) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from w  w w . j  a  va  2  s. c  o  m*/
     * 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
     * @return
     */
    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. appendLine(final StringBuilder s, final CharSequence line)
  2. appendStr(char splitTag, Object... strs)
  3. byteToString(StringBuilder sb, byte b)
  4. addStartLine(Object aObject, StringBuilder aResult)