Here you can find the source of normaliseWhitespace(String string)
Parameter | Description |
---|---|
string | content to normalise |
public static String normaliseWhitespace(String string)
//package com.java2s; public class Main { /**/* ww w . j av a 2s . co 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 * @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'; } }