Java String Join join(String delimiter, String... strings)

Here you can find the source of join(String delimiter, String... strings)

Description

Joins the given strings.

License

Open Source License

Parameter

Parameter Description
delimiter The delimiter to place between the individual strings.
strings The strings to be joined.

Return

Each of the strings in the given array delimited by the given string.

Declaration

public static String join(String delimiter, String... strings) 

Method Source Code

//package com.java2s;
/**//w ww  . java2s  .c om
 *  Copyright (C) 2002-2015   The FreeCol Team
 *
 *  This file is part of FreeCol.
 *
 *  FreeCol is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  FreeCol 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with FreeCol.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.List;

public class Main {
    /**
     * Joins the given strings.
     *
     * In Java 8, we can use String.join.
     *
     * @param delimiter The delimiter to place between the individual strings.
     * @param strings The strings to be joined.
     * @return Each of the strings in the given array delimited by the given
     *         string.
     */
    public static String join(String delimiter, String... strings) {
        if (strings == null || strings.length == 0) {
            return null;
        } else {
            StringBuilder result = new StringBuilder(strings[0]);
            for (int i = 1; i < strings.length; i++) {
                result.append(delimiter);
                result.append(strings[i]);
            }
            return result.toString();
        }
    }

    /**
     * Joins the given strings.
     *
     * @param delimiter The delimiter to place between the individual strings.
     * @param strings The strings to be joined.
     * @return Each of the strings in the given array delimited by the given
     *         string.
     */
    public static String join(String delimiter, List<String> strings) {
        return join(delimiter, strings.toArray(new String[0]));
    }
}

Related

  1. join(String delimiter, Iterable strings)
  2. join(String delimiter, Object... objects)
  3. join(String delimiter, Object... parts)
  4. join(String delimiter, String... items)
  5. join(String delimiter, String... params)
  6. join(String delimiter, T... array)
  7. join(String glue, Iterable items)
  8. join(String glue, String... str)
  9. join(String joiner, String... toJoin)