Here you can find the source of join(String joiner, List
Parameter | Description |
---|---|
joiner | The string to use as the delimiter between each element. |
joinees | The list of strings to join. |
public static String join(String joiner, List<String> joinees)
//package com.java2s; /*/*from w w w . j a va 2 s . c o m*/ * Copyright 2012 Splunk, Inc. * * 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.Arrays; import java.util.List; public class Main { /** * Joins the strings in {@code joinees}, separated by {@code joiner}. * * For example, {@code list} contains the strings {@code "a"}, {@code "b"}, * and {@code "c"}. To combine these strings as "a/b/c", use * {@code join("/", list)}. * * @param joiner The string to use as the delimiter between each element. * @param joinees The list of strings to join. * @return The combined strings in {@code joinees} separated by the * {@code joiner} delimiter. */ public static String join(String joiner, List<String> joinees) { if (joinees.isEmpty()) { return ""; } else { StringBuilder joined = new StringBuilder(); joined.append(joinees.get(0)); for (String s : joinees.subList(1, joinees.size())) { joined.append(joiner); joined.append(s); } return joined.toString(); } } /** * Joins the strings in {@code joinees}, separated by {@code joiner}. * * For example, {@code list} contains the strings {@code "a"}, {@code "b"}, * and {@code "c"}. To combine these strings as "a/b/c", use * {@code join("/", list)}. * * @param joiner The string to use as the delimiter between each element. * @param joinees The array of strings to join. * @return The combined strings in {@code joinees} separated by the * {@code joiner} delimiter. */ public static String join(String joiner, String[] joinees) { return join(joiner, Arrays.asList(joinees)); } }