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

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

Description

Joins string items into one string separated by delimiter.

License

Apache License

Parameter

Parameter Description
items array with items to be joined
delimiter separator that should be used as item delimiter. <code>null</code> or empty string mean no delimiter.

Return

all items joined into one string and separated by delimiter.

Declaration

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

Method Source Code


//package com.java2s;
/*/*from w  w  w . jav  a2  s .com*/
 * Copyright 2011-2012 Maxim Dominichenko
 * 
 * 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.
 */

import java.util.List;

public class Main {
    /**
     * Joins string items into one string separated by delimiter.
     *
     * @param items array with items to be joined
     * @param delimiter separator that should be used as item delimiter.
     *                  <code>null</code> or empty string mean no delimiter.
     * @return all items joined into one string and separated by delimiter.
     */
    public static String join(String[] items, String delimiter) {
        StringBuilder result = new StringBuilder();
        if (items != null)
            for (String item : items) {
                if (result.length() > 0 && !isEmpty(delimiter))
                    result.append(delimiter);
                result.append(item);
            }
        return result.toString();
    }

    /**
     * Joins string items into one string separated by delimiter.
     *
     * @param items list with items to be joined
     * @param delimiter separator that should be used as item delimiter.
     *                  <code>null</code> or empty string mean no delimiter.
     * @return all items joined into one string and separated by delimiter.
     */
    public static String join(List<?> items, String delimiter) {
        if (items == null || items.isEmpty())
            return "";
        StringBuilder result = new StringBuilder();
        boolean delimOk = !isEmpty(delimiter);
        for (Object item : items) {
            if (delimOk && result.length() > 0)
                result.append(delimiter);
            result.append(item);
        }
        return result.toString();
    }

    /**
     * Helper to check if the String is {@code null} or empty.<br/>
     * {@link String#isEmpty()} is not static and therefore require additional check for {@code null}.
     *
     * @param string A string to be checked
     * @return {@code true} if is not {@code null} and is not empty. {@code false} otherwise.
     */
    public static boolean isEmpty(String string) {
        return string == null || string.length() == 0;
    }
}

Related

  1. join(String[] arrayOfString)
  2. join(String[] datalines)
  3. join(String[] elements, String separator)
  4. join(String[] elements, String separator)
  5. join(String[] inputArray, String glueString)
  6. join(String[] lines, String delim)
  7. join(String[] lines, String sep)
  8. join(String[] lines, String separator)
  9. join(String[] names, String sep)