Java String Collapse collapse(String text)

Here you can find the source of collapse(String text)

Description

Collapses whitespace

License

Open Source License

Declaration

public static String collapse(String text) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   w  w w  .  j  a  v a 2s .  c  om*/
     * Collapses whitespace
     */
    public static String collapse(String text) {
        int len = text.length();

        // most of the texts are already in the collapsed form.
        // so look for the first whitespace in the hope that we will
        // never see it.
        int s = 0;
        while (s < len) {
            if (isWhiteSpace(text.charAt(s)))
                break;
            s++;
        }
        if (s == len)
            // the input happens to be already collapsed.
            return text;

        // we now know that the input contains spaces.
        // let's sit down and do the collapsing normally.

        StringBuilder result = new StringBuilder(len /*allocate enough size to avoid re-allocation*/);

        if (s != 0) {
            for (int i = 0; i < s; i++)
                result.append(text.charAt(i));
            result.append(' ');
        }

        boolean inStripMode = true;
        for (int i = s + 1; i < len; i++) {
            char ch = text.charAt(i);
            boolean b = isWhiteSpace(ch);
            if (inStripMode && b)
                continue; // skip this character

            inStripMode = b;
            if (inStripMode)
                result.append(' ');
            else
                result.append(ch);
        }

        // remove trailing whitespaces
        len = result.length();
        if (len > 0 && result.charAt(len - 1) == ' ')
            result.setLength(len - 1);
        // whitespaces are already collapsed,
        // so all we have to do is to remove the last one character
        // if it's a whitespace.

        return result.toString();
    }

    public static final boolean isWhiteSpace(char ch) {
        return ch == 0x9 || ch == 0xA || ch == 0xD || ch == 0x20;
    }
}

Related

  1. collapse(char c)
  2. collapse(double[] w)
  3. collapse(float[][] array)
  4. collapse(String str, String characters, String replacement)
  5. collapse(String t)
  6. collapsedName(Class klass)
  7. collapseLines(String message)
  8. collapseMultipleNewlinesToOne(String s)
  9. collapseName(String name)