Here you can find the source of appendNormalisedWhitespace(StringBuilder accum, String string, boolean stripLeading)
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 |
public static void appendNormalisedWhitespace(StringBuilder accum, String string, boolean stripLeading)
//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'; } }