Concatenates the given Collection<> and Object[] and puts 'glue' between them. - Java java.lang

Java examples for java.lang:String Join

Description

Concatenates the given Collection<> and Object[] and puts 'glue' between them.

Demo Code

//package com.java2s;

public class Main {
    /**/*from w  w w .ja  va2s.  co m*/
     * Concatenates the given parts and puts 'glue' between them.
     */
    public static String explode(
            java.util.Collection<? extends Object> parts, String glue) {
        return explode(parts.toArray(new Object[parts.size()]), glue);
    }

    /**
     * Concatenates the given parts and puts 'glue' between them.
     */
    public static String explode(Object[] parts, String glue) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < parts.length; i++) {
            Object next = parts[i];
            sb.append(next.toString());
            if (i < parts.length - 1) {
                sb.append(glue);
            }
        }
        return sb.toString();
    }
}

Related Tutorials