Java List Join join(List list, String delim)

Here you can find the source of join(List list, String delim)

Description

Creates a single string from a List of strings seperated by a delimiter.

License

Open Source License

Parameter

Parameter Description
list a list of strings to join
delim the delimiter character(s) to use. (null value will join with no delimiter)

Return

a String of all values in the list seperated by the delimiter

Declaration

public static String join(List<?> list, String delim) 

Method Source Code

//package com.java2s;
/*//from w ww.j a  va  2 s .  c om
 * Copyright (C) 2003-2011 eXo Platform SAS.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Creates a single string from a List of strings seperated by a delimiter.
     * @param list a list of strings to join
     * @param delim the delimiter character(s) to use. (null value will join with no delimiter)
     * @return a String of all values in the list seperated by the delimiter
     */
    public static String join(List<?> list, String delim) {
        if (list == null || list.size() < 1)
            return null;
        StringBuilder buf = new StringBuilder();
        Iterator<?> i = list.iterator();

        while (i.hasNext()) {
            buf.append(i.next());
            if (i.hasNext())
                buf.append(delim);
        }
        return buf.toString();
    }

    public static StringBuilder append(StringBuilder sb, Iterable<? extends Object> iterable, String prefix,
            String suffix, String sep) {
        return append(sb, iterable, prefix, suffix, null, sep, null);
    }

    public static StringBuilder append(StringBuilder sb, Iterable<? extends Object> iterable, String prefix,
            String suffix, String sepPrefix, String sep, String sepSuffix) {
        Iterator<? extends Object> it = iterable.iterator();
        while (it.hasNext()) {
            if (prefix != null)
                sb.append(prefix);
            sb.append(it.next());
            if (suffix != null)
                sb.append(suffix);
            if (it.hasNext() && sep != null) {
                if (sepPrefix != null)
                    sb.append(sepPrefix);
                sb.append(sep);
                if (sepSuffix != null)
                    sb.append(sepSuffix);
            }
        }
        return sb;
    }
}

Related

  1. join(List _items, String _delim)
  2. join(List arr, String seperator)
  3. join(List items, char separator)
  4. join(List items, String delimiter)
  5. join(List list)
  6. join(List list, String sep)
  7. join(List objects, String delimiter)
  8. join(List parts)
  9. join(List resultList, String sep)