Here you can find the source of joinStrings(List
strings
together in sequence with separator
between them.
Parameter | Description |
---|---|
strings | the strings to join together |
separator | the separator to include |
public static String joinStrings(List<String> strings, String separator)
//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(); } }