Java List Join joinStrings(List strings, String separator)

Here you can find the source of joinStrings(List strings, String separator)

Description

Join the strings in strings together in sequence with separator between them.

License

Open Source License

Parameter

Parameter Description
strings the strings to join together
separator the separator to include

Return

the joined strings

Declaration

public static String joinStrings(List<String> strings, String separator) 

Method Source Code

//package com.java2s;
/*//from   ww  w .  ja  v  a  2s  .c  om
 * Copyright 2013-2015 Cel Skeggs, 2016 Alexander Mackworth
 *
 * This file is part of the CCRE, the Common Chicken Runtime Engine.
 *
 * The CCRE 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 CCRE 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 CCRE.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.List;

public class Main {
    /**
     * Join the strings in <code>strings</code> together in sequence with
     * <code>separator</code> between them.
     *
     * For example, <code>["abc", "def", "hij"]</code> joined with
     * <code>"EJ"</code> as the separator would yield
     * <code>"abcEJdefEJhij"</code>.
     *
     * @param strings the strings to join together
     * @param separator the separator to include
     * @return the joined strings
     */
    public static String joinStrings(List<String> strings, String separator) {
        if (strings == null || separator == null) {
            throw new NullPointerException();
        }
        if (strings.isEmpty()) {
            return "";
        }

        StringBuilder builder = new StringBuilder(strings.get(0));
        for (String element : strings.subList(1, strings.size())) {
            if (element == null) {
                throw new NullPointerException();
            }
            builder.append(separator);
            builder.append(element);
        }
        return builder.toString();
    }
}

Related

  1. joinString(List val, String delim)
  2. joinStringList(String delimiter, List stringList)
  3. joinStrings(Iterable list)
  4. joinStrings(List aList, String aString)
  5. joinStrings(List list, String delim)
  6. joinStrings(List strings, String separator)
  7. joinStrings(String delim, List strings)
  8. joinStrings(String joiner, List objects)
  9. joinTextWithCommas(List list)