Android String Whitespace Normalize normaliseWhitespace(String string)

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

Description

normalise Whitespace

Declaration

public static String normaliseWhitespace(String string) 

Method Source Code

//package com.java2s;

public class Main {
    public static String normaliseWhitespace(String string) {
        StringBuilder sb = new StringBuilder(string.length());

        boolean lastWasWhite = false;
        boolean modified = false;

        int l = string.length();
        int c;/*from  w  ww .  j av a  2  s.com*/
        for (int i = 0; i < l; i += Character.charCount(c)) {
            c = string.codePointAt(i);
            if (isWhitespace(c)) {
                if (lastWasWhite) {
                    modified = true;
                    continue;
                }
                if (c != ' ')
                    modified = true;
                sb.append(' ');
                lastWasWhite = true;
            } else {
                sb.appendCodePoint(c);
                lastWasWhite = false;
            }
        }
        return modified ? sb.toString() : string;
    }

    /**
     * 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)