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

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

Description

Join strings with separator.

License

Open Source License

Parameter

Parameter Description
elements elements to join
separator placed between subsequent elements

Return

joined string elements

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Jarom?r H?ibal.//from   w  w w  . j a v a2  s  . c  om
 * 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 {
    /**
     * 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) {
        StringBuilder sb = new StringBuilder();
        int length = elements.length;
        for (int i = 0; i < length; i++) {
            sb.append(elements[i]);
            if (i + 1 < length) {
                sb.append(separator);
            }
        }
        return sb.toString();
    }
}

Related

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