Java String Collapse collapse(String t)

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

Description

collapse

License

Open Source License

Declaration

public static String collapse(String t) 

Method Source Code

//package com.java2s;
/**/*from   w ww . j a v  a  2 s. c o  m*/
 * Copyright 2004-2014 Riccardo Solmi. All rights reserved.
 * This file is part of the Whole Platform.
 *
 * The Whole Platform 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.
 *
 * The Whole Platform 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 the Whole Platform. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String collapse(String t) {
        for (int i = 0, length = t.length(); i < length; i++)
            if (!Character.isWhitespace(t.charAt(i)))
                for (int j = length - 1; j >= i; j--)
                    if (!Character.isWhitespace(t.charAt(j)))
                        return (i > 0 ? " " : "") + t.substring(i, j + 1) + (j < length - 1 ? " " : "");
        return "";
    }

    public static boolean isWhitespace(CharSequence cs) {
        int count = 0, length = cs.length();
        while (count < length && Character.isWhitespace(cs.charAt(count)))
            count++;
        return count == length;
    }
}

Related

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