Java List Join join(List strings, String delimiter)

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

Description

Join the elements of the set

License

Apache License

Parameter

Parameter Description
strings the list of elements
delimiter the delimiter

Return

the join string of the set elements

Declaration

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

Method Source Code

//package com.java2s;
/* /*from  w  w w .j  a  v  a2  s  .  c o  m*/
 * Copyright 2010 University of Southern California
 * 
 * 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;

import java.util.Set;

public class Main {
    /**
     * Join the elements of the set
     * 
     * @param strings
     *            the list of elements
     * @param delimiter
     *            the delimiter
     * @return the join string of the set elements
     */
    public static String join(List<String> strings, String delimiter) {
        if (strings == null || delimiter == null) {
            return "";
        }

        StringBuffer buf = new StringBuffer();
        boolean first = true;

        for (String value : strings) {
            if (first) {
                first = false;
            } else {
                buf.append(delimiter);
            }
            buf.append(value);
        }

        return buf.toString();
    }

    /**
     * Join the elements of the set
     * 
     * @param strings
     *            the set of elements
     * @param delimiter
     *            the delimiter
     * @return the join string of the set elements
     */
    public static String join(Set<String> strings, String delimiter) {
        if (strings == null || delimiter == null) {
            return "";
        }

        StringBuffer buf = new StringBuffer();
        boolean first = true;

        for (String value : strings) {
            if (first) {
                first = false;
            } else {
                buf.append(delimiter);
            }
            buf.append(value);
        }

        return buf.toString();
    }
}

Related

  1. join(List s, String sep)
  2. join(List strings)
  3. join(List strings)
  4. join(List strings)
  5. join(List strings, String delim)
  6. join(List strings, String join)
  7. join(List strings, String joiner)
  8. join(List strings, String sep)
  9. join(List strings, String separator)