Here you can find the source of join(final List
Parameter | Description |
---|---|
substrings | List of substrings |
join | Join to place between each element |
public static String join(final List<String> substrings, final String join)
//package com.java2s; /*//from w w w .j av a 2s .c o m * Copyright 2014 Click Travel Ltd * * 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 a list of substrings with a join between each element * @param substrings List of substrings * @param join Join to place between each element * @return Joined string */ public static String join(final List<String> substrings, final String join) { final StringBuilder sb = new StringBuilder(); for (final String substring : substrings) { if (sb.length() != 0) { sb.append(join); } sb.append(substring); } return sb.toString(); } /** * Joins a list of substring with commas * @param substrings List of substrings * @return Joined string */ public static String join(final List<String> substrings) { return join(substrings, ", "); } }