Java String Whitespace Collapse collapseWhitespace(String string)

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

Description

collapse Whitespace

License

Open Source License

Declaration

public static String collapseWhitespace(String string) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 United States Government as represented by the
 * Administrator of the National Aeronautics and Space Administration.
 * All Rights Reserved.//from   w ww . j  av  a  2 s  .  c o  m
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

public class Main {
    public static String collapseWhitespace(String string) {
        if (string.isEmpty()) {
            return string;
        }
        char space = ' ';
        char newline = '\n';
        if (string.indexOf(newline) == -1 && string.indexOf('\r') != -1) {
            newline = '\r';
        }
        StringBuilder s = new StringBuilder(string.length());
        char previousChar = newline;
        for (char currentChar : string.toCharArray()) {
            // Collapse multiple newlines.
            if (currentChar == newline && previousChar == newline)
                continue;
            // Collapse multiple spaces.
            if (currentChar == space && previousChar == space)
                continue;
            // Remove whitespace at beginning of line (indentation).
            if (currentChar == space && previousChar == newline)
                continue;
            // At end of line, if previous line ended with a space
            // (or spaces, but they collapse into one), go back and remove it.
            if (currentChar == newline && previousChar == space) {
                s.deleteCharAt(s.length() - 1);
            }
            s.append(currentChar);
            previousChar = currentChar;
        }
        // At end of string, if previous line ended with a space or newline
        // (or multiple, but they collapse into one), go back and remove it.
        if (previousChar == space || previousChar == newline) {
            s.deleteCharAt(s.length() - 1);
        }
        return s.toString();
    }
}

Related

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