Java String Whitespace Collapse collapseSpaces(String s)

Here you can find the source of collapseSpaces(String s)

Description

collapse Spaces

License

Open Source License

Declaration

public static String collapseSpaces(String s) 

Method Source Code

//package com.java2s;
/*//from  w w  w  . ja v  a 2  s.  c om
 * ====================================================================
 * Copyright (c) 2004-2012 TMate Software Ltd.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://svnkit.com/license.html
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 * ====================================================================
 */

public class Main {
    public static String collapseSpaces(String s) {
        final StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            final char c = s.charAt(i);
            if (!isSpace(c)) {
                stringBuilder.append(c);
            }
        }
        return stringBuilder.toString();
    }

    public static boolean isSpace(char c) {
        return c == ' ' || c == '\t' || c == '\n' || c == 0x0b || c == '\f' || c == '\r';
    }
}

Related

  1. collapseSpaces(String argStr)
  2. collapseSpaces(String text, char[] spaceCharacters)
  3. collapseWhiteSpace(CharSequence value)
  4. collapseWhiteSpace(final String s)
  5. collapseWhiteSpace(String content)