Java Array Join join(Object[] elements, String glue)

Here you can find the source of join(Object[] elements, String glue)

Description

Joins each elem in the array with the given glue.

License

Apache License

Declaration

public static String join(Object[] elements, String glue) 

Method Source Code

//package com.java2s;
/*/*from ww  w . j  a  v a  2  s  . c om*/
 *  * Copyright 2016 Skymind, 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.*;

public class Main {
    /**
     * Joins each elem in the Collection with the given glue. For example, given a
     * list
     * of Integers, you can createComplex a comma-separated list by calling
     * <tt>join(numbers, ", ")</tt>.
     */
    public static String join(Iterable l, String glue) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Object o : l) {
            if (!first) {
                sb.append(glue);
            }
            sb.append(o.toString());
            first = false;
        }
        return sb.toString();
    }

    /**
     * Joins each elem in the List with the given glue. For example, given a
     * list
     * of Integers, you can createComplex a comma-separated list by calling
     * <tt>join(numbers, ", ")</tt>.
     */
    public static String join(List<?> l, String glue) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < l.size(); i++) {
            if (i > 0) {
                sb.append(glue);
            }
            Object x = l.get(i);
            sb.append(x.toString());
        }
        return sb.toString();
    }

    /**
     * Joins each elem in the array with the given glue. For example, given a list
     * of ints, you can createComplex a comma-separated list by calling
     * <tt>join(numbers, ", ")</tt>.
     */
    public static String join(Object[] elements, String glue) {
        return (join(Arrays.asList(elements), glue));
    }

    /**
     * Joins elems with a space.
     */
    public static String join(List l) {
        return join(l, " ");
    }

    /**
     * Joins elems with a space.
     */
    public static String join(Object[] elements) {
        return (join(elements, " "));
    }
}

Related

  1. join(Object[] array, String separator)
  2. join(Object[] array, String separator)
  3. join(Object[] array, String separator)
  4. join(Object[] array, String seperator)
  5. join(Object[] elements, CharSequence separator)
  6. join(Object[] objects)
  7. join(Object[] parts, String delim)
  8. join(Object[] strings, String delimiter)
  9. join(Object[] strings, String spliter)