Java String Whitespace Collapse collapseWhiteSpace(CharSequence value)

Here you can find the source of collapseWhiteSpace(CharSequence value)

Description

Replaces successive XML space characters by a single space character (' ') then removes leading and trailing space characters if any.

License

Open Source License

Parameter

Parameter Description
value string to be processed

Return

processed string

Declaration

public static final String collapseWhiteSpace(CharSequence value) 

Method Source Code

//package com.java2s;
/*/*  ww  w.  j  a v a  2 s .c o m*/
 *    Qizx/open 4.1
 *
 * This code is the open-source version of Qizx.
 * Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
 *
 * The contents of this file are subject to the Mozilla Public License 
 *  Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 *  for the specific language governing rights and limitations under the
 *  License.
 *
 * The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
 *
 */

public class Main {
    /**
     * Replaces successive XML space characters by a single space character (<tt>' '</tt>)
     * then removes leading and trailing space characters if any.
     * 
     * @param value string to be processed
     * @return processed string
     */
    public static final String collapseWhiteSpace(CharSequence value) {
        StringBuffer buffer = new StringBuffer();
        compressWhiteSpace(value, buffer);

        int last = buffer.length() - 1;
        if (last >= 0) {
            if (buffer.charAt(last) == ' ') {
                buffer.deleteCharAt(last);
                --last;
            }

            if (last >= 0 && buffer.charAt(0) == ' ')
                buffer.deleteCharAt(0);
        }

        return buffer.toString();
    }

    /**
     * Replaces successive XML space characters (<tt>'\t'</tt>,
     * <tt>'\r'</tt>, <tt>'\n'</tt>, <tt>' '</tt>) by a single space
     * character (<tt>' '</tt>).
     * 
     * @param value string to be processed
     * @return processed string
     */
    public static final String compressWhiteSpace(String value) {
        StringBuffer buffer = new StringBuffer();
        compressWhiteSpace(value, buffer);
        return buffer.toString();
    }

    /**
     * Replaces successive XML space characters (<tt>'\t'</tt>,
     * <tt>'\r'</tt>, <tt>'\n'</tt>, <tt>' '</tt>) by a single space
     * character (<tt>' '</tt>).
     * 
     * @param value string to be processed
     * @param buffer buffer used to store processed characters (characters are
     *        appended to this buffer)
     */
    private static void compressWhiteSpace(CharSequence value, StringBuffer buffer) {
        // No need to convert "\r\n" to a single '\n' because white spaces
        // are compressed.

        int length = value.length();
        char prevChar = '?';
        for (int i = 0; i < length; ++i) {
            char c = value.charAt(i);

            switch (c) {
            case '\t':
            case '\r':
            case '\n':
                c = ' ';
                break;
            }

            if (c == ' ') {
                if (prevChar != ' ') {
                    buffer.append(c);
                    prevChar = c;
                }
            } else {
                buffer.append(c);
                prevChar = c;
            }
        }
    }
}

Related

  1. collapseSpaces(String argStr)
  2. collapseSpaces(String s)
  3. collapseSpaces(String text, char[] spaceCharacters)
  4. collapseWhiteSpace(final String s)
  5. collapseWhiteSpace(String content)
  6. collapseWhitespace(String in, char replacementChar)
  7. collapseWhitespace(String s)