Java List Join join(final List substrings, final String join)

Here you can find the source of join(final List substrings, final String join)

Description

Joins a list of substrings with a join between each element

License

Apache License

Parameter

Parameter Description
substrings List of substrings
join Join to place between each element

Return

Joined string

Declaration

public static String join(final List<String> substrings, final String join) 

Method Source Code

//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, ", ");
    }
}

Related

  1. join(final List list)
  2. join(final List list, final String sepa)
  3. join(final List path, final char delimiter)
  4. join(final List strings, final String delimiter)
  5. join(final List strings, final String separator)
  6. join(final String delimiter, List values)
  7. join(final String separator, final List list)
  8. join(final String with, final List array)
  9. join(Iterable list)