Java String Join join(String delim, Object... objects)

Here you can find the source of join(String delim, Object... objects)

Description

Join a list of non-null objects with delim and return it as a string.

License

Open Source License

Declaration

public static String join(String delim, Object... objects) 

Method Source Code

//package com.java2s;
/*//from w w  w. j a  v  a2  s . c  o m
 * Carrot2 project.
 *
 * Copyright (C) 2002-2016, Dawid Weiss, Stanis?aw Osi?ski.
 * All rights reserved.
 *
 * Refer to the full license file "carrot2.LICENSE"
 * in the root folder of the repository checkout or at:
 * http://www.carrot2.org/carrot2.LICENSE
 */

import java.util.Iterator;

public class Main {
    /**
     * Join a list of non-null objects with <code>delim</code> and return it
     * as a string.
     */
    public static String join(String delim, Object... objects) {
        final StringBuilder b = new StringBuilder();
        for (Object s : objects) {
            if (s != null) {
                if (b.length() > 0)
                    b.append(delim);
                b.append(s.toString());
            }
        }
        return b.toString();
    }

    public static <T> String toString(Iterable<T> iterable, String separator) {
        final StringBuilder stringBuilder = new StringBuilder();

        for (final Iterator<T> iterator = iterable.iterator(); iterator.hasNext();) {
            final T object = iterator.next();
            stringBuilder.append(object);
            if (iterator.hasNext()) {
                stringBuilder.append(separator);
            }
        }

        return stringBuilder.toString();
    }
}

Related

  1. join(Iterable input)
  2. join(java.util.Collection strings, String delimiter)
  3. join(Map map, String separator)
  4. join(String connector, Object... objects)
  5. join(String delim, List strings)
  6. join(String delimiter, Collection objects)
  7. join(String delimiter, Collection items)
  8. join(String delimiter, Iterable elements)
  9. join(String delimiter, Iterable strings)