Java Array Join join(String[] stringArray, String delimiter)

Here you can find the source of join(String[] stringArray, String delimiter)

Description

join

License

BSD License

Declaration

public static String join(String[] stringArray, String delimiter) 

Method Source Code


//package com.java2s;
/*L/*from   w  w w .  j a v a  2  s .c o  m*/
 *  Copyright SAIC
 *  Copyright SAIC-Frederick
 *
 *  Distributed under the OSI-approved BSD 3-Clause License.
 *  See http://ncip.github.com/cananolab/LICENSE.txt for details.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.List;

public class Main {
    public static String join(String[] stringArray, String delimiter) {
        String joinedStr = "";
        if (stringArray == null || stringArray.length == 0) {
            return joinedStr;
        }
        List<String> stringList = Arrays.asList(stringArray);
        return join(stringList, delimiter);
    }

    public static String join(Collection<String> stringList, String delimiter) {
        String joinedStr = "";
        if (stringList == null || stringList.isEmpty()) {
            return joinedStr;
        }
        // remove empty items
        Collection<String> modList = new ArrayList<String>(stringList);
        for (String str : modList) {
            if (isEmpty(str)) {
                stringList.remove(str);
            }
        }

        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (String str : stringList) {
            if (i < stringList.size() - 1) {
                sb.append(str);
                sb.append(delimiter);
            } else {
                sb.append(str);
            }
            i++;
        }
        joinedStr = sb.toString();
        return joinedStr;
    }

    /**
     * Return true for Null or empty string, false otherwise.
     */
    public static boolean isEmpty(String str) {
        return (str == null || str.trim().length() == 0);
    }
}

Related

  1. join(String[] parts, String separator)
  2. join(String[] sentence, String separator)
  3. join(String[] strArr)
  4. join(String[] strArray, String delimiter)
  5. join(String[] stringArray, boolean quote, String glue)
  6. join(String[] stringArray, String delimiterString, boolean joinNullValues)
  7. join(String[] stringArray, String separator)
  8. join(String[] strings)
  9. join(String[] strings, char delimiter)