Java Array Implode implode(Object[] array, String separator)

Here you can find the source of implode(Object[] array, String separator)

Description

implode

License

Open Source License

Declaration

public static String implode(Object[] array, String separator) 

Method Source Code

//package com.java2s;
/***************************************************************************
 * Copyright (c) 2006 Eike Stepper, Fuggerstr. 39, 10777 Berlin, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //from ww w  .  jav  a 2s .com
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static String implode(Collection collection, String separator) {
        if (collection == null)
            return null;
        if (collection.size() == 0)
            return "";

        Iterator iter = collection.iterator();
        StringBuffer result = new StringBuffer(iter.next().toString());

        while (iter.hasNext()) {
            if (separator != null)
                result.append(separator);
            result.append(iter.next().toString());
        }

        return result.toString();
    }

    public static String implode(Object[] array, String separator) {
        if (array == null)
            return null;
        if (array.length == 0)
            return "";

        StringBuffer result = new StringBuffer(array[0].toString());

        for (int i = 1; i < array.length; i++) {
            if (separator != null)
                result.append(separator);
            result.append(array[i].toString());
        }

        return result.toString();
    }
}

Related

  1. implode(double[] array, String separator)
  2. implode(final String[] array, final String delim)
  3. implode(final String[] pStrArray)
  4. implode(Object[] ary, String delim)
  5. implode(String delim, Object[] objects)
  6. implode(String delim, String[] args)
  7. implode(String[] array, String separator)