Java String Whitespace Collapse collapseWhiteSpace(String string)

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

Description

After the processing implied by replace, contiguous sequences of #x20's are collapsed to a single #x20, and leading and trailing #x20's are removed.

License

Open Source License

Parameter

Parameter Description
string a parameter

Return

the string with collapsed whitespace, null if string is null.

Declaration

public static String collapseWhiteSpace(String string) 

Method Source Code

//package com.java2s;
/*/*w  ww  .j a va2  s  .  co m*/
 * sulky-modules - several general-purpose modules.
 * Copyright (C) 2007-2008 Joern Huxhorn
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    private static final char TAB = 0x09;
    private static final char LINE_FEED = 0x0A;
    private static final char CARRIAGE_RETURN = 0x0D;
    private static final char SPACE = 0x20;

    /**
     * After the processing implied by replace, contiguous sequences of #x20's
     * are collapsed to a single #x20, and leading and trailing #x20's are
     * removed.
     * 
     * @param string
     * @return the string with collapsed whitespace, null if string is null.
     * @see #replaceWhiteSpace(String)
     */
    public static String collapseWhiteSpace(String string) {
        if (string == null) {
            return null;
        }
        String replaced = replaceWhiteSpace(string);
        char[] chars = replaced.toCharArray();
        StringBuffer result = new StringBuffer(replaced.length());
        boolean needSpace = false;
        for (char c : chars) {
            if (c == SPACE) {
                if (result.length() != 0) {
                    // we are not at the start anymore
                    needSpace = true;
                }
            } else {
                if (needSpace) {
                    needSpace = false;
                    result.append(SPACE);
                }
                result.append(c);
            }
        }
        return result.toString();
    }

    /**
     * All occurrences of #x9 (tab), #xA (line feed) and #xD (carriage return)
     * are replaced with #x20 (space)
     * 
     * @param string
     * @return the string with replaced whitespace, null if string is null.
     */
    public static String replaceWhiteSpace(String string) {
        if (string == null) {
            return null;
        }
        StringBuffer result = new StringBuffer(string);
        for (int i = 0; i < result.length(); i++) {
            char c = result.charAt(i);
            if (c == TAB || c == CARRIAGE_RETURN || c == LINE_FEED) {
                result.setCharAt(i, SPACE);
            }
        }
        return result.toString();
    }
}

Related

  1. collapseWhitespace(String in, char replacementChar)
  2. collapseWhitespace(String s)
  3. collapseWhitespace(String s, boolean trim)
  4. collapseWhitespace(String str)
  5. collapseWhitespace(String string)
  6. collapseWhiteSpace(String text)
  7. collapseWhitespace(String text)
  8. collapseWhiteSpace(StringBuffer oriText)
  9. collapseWhiteSpaces(String str)