Java List Join join(List strings, String separator)

Here you can find the source of join(List strings, String separator)

Description

Joins a list of strings.

License

CDDL license

Parameter

Parameter Description
strings the list of strings; can be empty, but not null.
separator a separator; can be null.

Return

a string containing the joined strings; never null.

Declaration

public static String join(List<String> strings, String separator) 

Method Source Code

//package com.java2s;
/*/*from   ww  w.  j  a v a2 s  .c o m*/
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
 * or http://www.netbeans.org/cddl.txt.
 *
 * When distributing Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://www.netbeans.org/cddl.txt.
 * If applicable, add the following below the CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * The Original Software is NetBeans. The Initial Developer of the Original
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

import java.util.List;

public class Main {
    /**
     * Joins a list of strings.
     *
     * @param strings the list of strings; can be empty, but not null.
     * @param separator a separator; can be null.
     * @return a string containing the joined strings; never null.
     */
    public static String join(List<String> strings, String separator) {
        int size = strings.size();
        if (size == 0) {
            return ""; // NOI18N
        }
        StringBuilder sb = new StringBuilder(strings.size() * strings.get(0).length());
        int index = 0;
        int lastIndex = size - 1;
        for (String string : strings) {
            sb.append(string);
            if (separator != null && index < lastIndex) {
                sb.append(separator);
            }
            index++;
        }
        return sb.toString();
    }
}

Related

  1. join(List strings, String join)
  2. join(List strings, String joiner)
  3. join(List strings, String sep)
  4. join(List strings, String separator)
  5. join(List strings, String separator)
  6. join(List strings, String separator)
  7. join(List strings, String separator)
  8. join(List stringsToJoin, char delimiter)
  9. join(List strList, char separator)