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

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

Description

join

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Jarom?r H?ibal./*from   www  . j  ava 2  s  .  c o  m*/
 * 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
 *
 * Contributors:
 *     Jarom?r H?ibal <jaromirhribal@gmail.com> - initial API and implementation
 *******************************************************************************/

public class Main {
    public static String join(String[] elements, String separator) {
        return join(elements, separator, 0);
    }

    /**
     * Join strings with separator.
     * <b>Example:</b>
     * elements: <code>java, util, ArrayList</code>
     * separator: <code>.</code>
     * returns: <code>java.util.ArrayList</code>
     * 
     * @param elements elements to join
     * @param separator placed between subsequent elements
     * @return joined string elements
     */
    public static String join(String[] elements, String separator, int startIndex) {

        StringBuilder sb = new StringBuilder();

        int len = elements.length;

        if (len - startIndex <= 0)
            return "";

        int cond = len - 1;

        for (int i = startIndex; i < cond; i++) {
            sb.append(elements[i]);
            sb.append(separator);
        }

        sb.append(elements[cond]);

        return sb.toString();
    }
}

Related

  1. join(String[] array, String separator)
  2. join(String[] array, String separator)
  3. join(String[] array, String separator)
  4. join(String[] arrayOfString)
  5. join(String[] datalines)
  6. join(String[] elements, String separator)
  7. join(String[] inputArray, String glueString)
  8. join(String[] items, String delimiter)
  9. join(String[] lines, String delim)