Java Array Flatten flatten(String s[])

Here you can find the source of flatten(String s[])

Description

Shorthand for #flatten(String[],String) invoked with a " " separator.

License

Open Source License

Parameter

Parameter Description
s the string array to flatten

Return

the flattened string array

Declaration

public static String flatten(String s[]) 

Method Source Code

//package com.java2s;
/*//w  w  w  . j  ava2  s  .  c om
 * This file is part of muCommander, http://www.mucommander.com
 * Copyright (C) 2002-2010 Maxence Bernard
 *
 * muCommander 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.
 *
 * muCommander 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 {
    /**
     * Shorthand for {@link #flatten(String[], String)} invoked with a <code>" "</code> separator.
     *
     * @param s the string array to flatten
     * @return the flattened string array
     */
    public static String flatten(String s[]) {
        return flatten(s, " ");
    }

    /**
     * Concatenates all of the given string array's elements into a single string, separating each element
     * by the specified separator string. <code>null</code> and empty string values are simply skipped and not
     * reflected in the returned string. If the string array is <code>null</code>, the returned string will also be
     * <code>null</code>.
     *
     * @param s the string array to flatten
     * @param separator the String that separates each
     * @return the flattened string array
     */
    public static String flatten(String s[], String separator) {
        if (s == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        boolean first = true;

        for (String el : s) {
            if (isNullOrEmpty(el)) {
                continue;
            }

            if (first) {
                first = false;
            } else {
                sb.append(separator);
            }

            sb.append(el);
        }

        return sb.toString();
    }

    /**
     * Returns true if the given string is null or empty (i.e. it's length is 0)
     *
     * @param string - the given String to check
     * @return true if the given string is null or empty, false otherwise
     */
    public static boolean isNullOrEmpty(String string) {
        return string == null || string.isEmpty();
    }
}

Related

  1. flatten(final Object[] array)
  2. flatten(float[][] mat)
  3. flatten(Object[] array)
  4. flatten(Object[] array)
  5. flatten(Object[] lines, String sep)
  6. flatten(String[] strings, String separator)
  7. flattenArguments(String[] arguments)
  8. flattenArray(Object... objects)
  9. flattenArray(Object[] words)