Java String Join join(String sep, Object... pieces)

Here you can find the source of join(String sep, Object... pieces)

Description

Turns a sequence of objects into a string, delimited by sep .

License

Apache License

Parameter

Parameter Description
sep the string separator to delimit the different output segments.
pieces A Collection or a varargs Array of objects.

Declaration

@SuppressWarnings("unchecked")
public static String join(String sep, Object... pieces) 

Method Source Code

//package com.java2s;
/*// w  w w . ja  va2s .  c om
 * Copyright (C) 2011 The Android Open Source Project
 *
 * 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.Collection;
import java.util.Iterator;

public class Main {
    /**
     * Turns a sequence of objects into a string, delimited by {@code sep}.  If a single
     * {@code Collection} is passed, it is assumed that the elements of that Collection are to be
     * joined.  Otherwise, wraps the passed {@link Object}(s) in a {@link List} and joins the
     * generated list.
     *
     * @param sep the string separator to delimit the different output segments.
     * @param pieces A {@link Collection} or a varargs {@code Array} of objects.
     */
    @SuppressWarnings("unchecked")
    public static String join(String sep, Object... pieces) {
        if ((pieces.length == 1) && (pieces[0] instanceof Collection)) {
            // Don't re-wrap the Collection
            return internalJoin(sep, (Collection<Object>) pieces[0]);
        } else {
            return internalJoin(sep, Arrays.asList(pieces));
        }
    }

    private static String internalJoin(String sep, Collection<Object> pieces) {
        StringBuilder sb = new StringBuilder();
        boolean skipSep = true;
        Iterator<Object> iter = pieces.iterator();
        while (iter.hasNext()) {
            if (skipSep) {
                skipSep = false;
            } else {
                sb.append(sep);
            }

            Object obj = iter.next();
            if (obj == null) {
                sb.append("null");
            } else {
                sb.append(obj.toString());
            }
        }
        return sb.toString();
    }
}

Related

  1. join(String delimiter, T... array)
  2. join(String glue, Iterable items)
  3. join(String glue, String... str)
  4. join(String joiner, String... toJoin)
  5. join(String s, Object... objects)
  6. join(String separator, CharSequence... elements)
  7. join(String separator, Collection strings)
  8. join(String separator, List objects)
  9. join(String separator, Object... objects)