Java List Join join(String separator, List objs)

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

Description

Join a list of Strings with a separator.

License

Apache License

Declaration

public static String join(String separator, List<? extends Object> objs) 

Method Source Code

//package com.java2s;
/**//from   w w w . j  av  a  2s  .c  o  m
 *  Copyright 2009 Welocalize, Inc.
 *
 *  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.Arrays;

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

public class Main {
    public static final String EMPTY_STRING = "";

    /**
     * Join a list of Strings with a separator.  Objects are converted with
     * toString.  In the special case where the list is empty, the empty string
     * is returned.
     *
     * Be aware that splitting on the separator may not return the original
     * list of Strings.  This happens if the list is empty, or the separator
     * appears in one of the elements.
     */
    public static String join(String separator, List<? extends Object> objs) {
        if (objs == null) {
            throw new IllegalArgumentException("objs is null");
        }
        if (objs.isEmpty()) {
            return EMPTY_STRING;
        }
        Iterator<? extends Object> i = objs.iterator();
        StringBuilder r = new StringBuilder(i.next().toString());
        while (i.hasNext()) {
            r.append(separator);
            r.append(i.next().toString());
        }
        return r.toString();
    }

    /** @see #join(String, List) */
    public static String join(String separator, Object... objs) {
        return join(separator, Arrays.asList(objs));
    }

    public static boolean isEmpty(String s) {
        return s == null || s.trim().length() == 0;
    }
}

Related

  1. join(String joint, List elements)
  2. join(String sep, List a)
  3. join(String separator, Collection list)
  4. join(String separator, Collection inputList)
  5. join(String separator, List items)
  6. join(String separator, List elements)
  7. join(String separator, List items)
  8. join(String separator, List parts)
  9. join(String separator, List parts)