Java Array Join join(String[] lines, String separator)

Here you can find the source of join(String[] lines, String separator)

Description

join

License

Open Source License

Parameter

Parameter Description
lines a parameter
separator a parameter

Declaration

public static String join(String[] lines, String separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005, 2016 IBM Corporation and others.
 * 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
 *
 *******************************************************************************/

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

public class Main {
    /**//from   w w w .  jav  a  2  s .co m
     * <p>
     * Joins the elements of the provided <code>Collection</code> into a single
     * String containing the provided elements.
     * </p>
     * 
     * <p>
     * No delimiter is added before or after the list. A <code>null</code>
     * separator is the same as an empty String ("").
     * </p>
     * 
     * @param collection
     *            the <code>Collection</code> of values to join together, may be
     *            null
     * @param separator
     *            the separator character to use, null treated as ""
     * @return the joined String, <code>null</code> if null collection input
     */
    public static String join(Collection<?> collection, String separator) {
        // handle null, zero and one elements before building a buffer
        if (collection == null) {
            return null;
        }
        if (collection.isEmpty()) {
            return ""; //$NON-NLS-1$
        }
        final Iterator<?> iterator = collection.iterator();
        final Object first = iterator.next();
        if (!iterator.hasNext()) {
            return first != null ? first.toString() : ""; //$NON-NLS-1$
        }
        // two or more elements
        final StringBuffer buf = new StringBuffer(256);
        if (first != null) {
            buf.append(first);
        }
        while (iterator.hasNext()) {
            if (separator != null) {
                buf.append(separator);
            }
            final Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }

    /**
     * <p>
     * Joins the elements of the provided <code>Collection</code> into a single
     * String containing the provided elements.
     * </p>
     * 
     * <p>
     * No delimiter is added before or after the list. A <code>null</code>
     * separator is the same as an empty String ("").
     * </p>
     * 
     * @param collection
     *            the <code>Collection</code> of values to join together, may be
     *            null
     * @param separator
     *            the separator character to use, null treated as ""
     * @return the joined String, <code>null</code> if null collection input
     */
    public static String join(Collection<?> collection, char separator) {
        // handle null, zero and one elements before building a buffer
        if (collection == null) {
            return null;
        }
        if (collection.isEmpty()) {
            return ""; //$NON-NLS-1$
        }
        final Iterator<?> iterator = collection.iterator();
        final Object first = iterator.next();
        if (!iterator.hasNext()) {
            return first != null ? first.toString() : ""; //$NON-NLS-1$
        }
        // two or more elements
        final StringBuffer buf = new StringBuffer(256);
        if (first != null) {
            buf.append(first);
        }
        while (iterator.hasNext()) {
            buf.append(separator);
            final Object obj = iterator.next();
            if (obj != null) {
                buf.append(obj);
            }
        }
        return buf.toString();
    }

    /**
     * @param lines
     * @param separator
     * @return
     */
    public static String join(String[] lines, char separator) {
        final StringBuffer sb = new StringBuffer(256);
        for (int i = 0; i < lines.length; ++i) {
            if (i != 0) {
                sb.append(separator);
            }
            sb.append(lines[i]);
        }
        return sb.toString();
    }

    /**
     * @param lines
     * @param separator
     * @return
     * @since 2.0
     */
    public static String join(String[] lines, String separator) {
        final StringBuffer sb = new StringBuffer(256);
        for (int i = 0; i < lines.length; ++i) {
            if (i != 0) {
                sb.append(separator);
            }
            sb.append(lines[i]);
        }
        return sb.toString();
    }
}

Related

  1. join(String[] elements, String separator)
  2. join(String[] inputArray, String glueString)
  3. join(String[] items, String delimiter)
  4. join(String[] lines, String delim)
  5. join(String[] lines, String sep)
  6. join(String[] names, String sep)
  7. join(String[] parts, String separator)
  8. join(String[] sentence, String separator)
  9. join(String[] strArr)