Java List Join join(List elements, String sep)

Here you can find the source of join(List elements, String sep)

Description

"join" a List of Strings into a single string, with each string separated by a defined separator string.

License

Open Source License

Parameter

Parameter Description
elements the strings to join together
sep the separator string

Return

the strings joined together

Declaration

public static String join(List elements, String sep) 

Method Source Code

//package com.java2s;
/*/*w  ww.jav a2s . c o m*/
 * Copyright (C) 2001-2004 Red Hat, Inc. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.util.List;
import java.util.Iterator;

public class Main {
    /**
     * "join" a List of Strings into a single string, with each string
     * separated by a defined separator string.
     * @deprecated use {@link com.arsdigita.util.StringUtils}
     *
     * @param elements the strings to join together
     * @param sep the separator string
     * @return the strings joined together
     */
    public static String join(List elements, String sep) {
        StringBuffer sb = new StringBuffer();
        boolean first = true;
        Iterator iter = elements.iterator();

        while (iter.hasNext()) {
            String element = (String) iter.next();

            if (!first) {
                sb.append(sep);
            } else {
                first = false;
            }

            sb.append(element);
        }

        return sb.toString();
    }
}

Related

  1. join(Iterable list, String delimiter)
  2. join(List a, List b)
  3. join(List a_quotedArgs)
  4. join(List array, String separator)
  5. join(List elements, String joinWith)
  6. join(List items, String separator)
  7. join(List l, String sep)
  8. join(List list, char separator)
  9. join(List list, String delim)