Join array elements with a glue String. - Java java.lang

Java examples for java.lang:String Join

Description

Join array elements with a glue String.

Demo Code


//package com.java2s;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static void main(String[] argv) {
        String glue = "java2s.com";
        int[] pieces = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(implode(glue, pieces));
    }//from   w w w.  ja  va2  s  . c  o  m

    /**
     * Join array elements with a glue String.
     *
     * @param glue
     * the separator
     * @param pieces
     * the parts to join
     * @return a String containing a String representation of all the array
     * elements in the same order, with the glue String between each
     * element.
     */
    public static String implode(final String glue, final int[] pieces) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < pieces.length; i++) {
            if (i != 0) {
                sb.append(glue);
            }
            sb.append(pieces[i]);
        }
        return sb.toString();
    }

    /**
     * Join array elements with a glue String.
     *
     * @param glue
     * the separator
     * @param pieces
     * the parts to join
     * @return a String containing a String representation of all the array
     * elements in the same order, with the glue String between each
     * element.
     */
    public static String implode(final String glue, final String[] pieces) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < pieces.length; i++) {
            if (i != 0) {
                sb.append(glue);
            }
            sb.append(pieces[i]);
        }
        return sb.toString();
    }

    /**
     * Join collection elements with a glue String.
     *
     * @param glue
     * the separator
     * @param pieces
     * the parts to join
     * @return a String containing a String representation of all the
     * Collection elements, with the glue String between each
     * element.
     */
    public static String implode(final String glue,
            final Collection<?> pieces) {
        StringBuilder sb = new StringBuilder();
        boolean firstElement = true;
        for (Object element : pieces) {
            if (!firstElement) {
                sb.append(glue);
            }
            sb.append(element);
            firstElement = false;
        }
        return sb.toString();
    }

    /**
     * Join keys elements of the HashMap with a glue String.
     *
     * @param glue
     * the separator
     * @param pieces
     * the parts to join
     * @return a String containing a String representation of all the array
     * elements in the same order, with the glue String between each
     * element.
     */
    public static String implode(final String glue,
            final Map<String, Object> pieces) {
        String sep = null;
        StringBuilder sb = new StringBuilder();
        Iterator<Entry<String, Object>> it = pieces.entrySet().iterator();
        while (it.hasNext()) {
            if (sep != null) {
                sb.append(sep);
            }
            sb.append((it.next()).getKey());
            sep = glue;
        }
        return sb.toString();
    }
}

Related Tutorials