Java List Join join(String[] elementList, String separator)

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

Description

Join the given element list with the specified separator

License

Apache License

Parameter

Parameter Description
elementList a list of strings to join
separator e.g., ",", " ", etc.

Return

the concatination of the strings in the elementList, separated by the separator

Declaration

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

Method Source Code


//package com.java2s;
/*--------------------------------------------------------------------------
 *  Copyright 2007 utgenome.org// w w  w.  j  ava2  s  . c  o  m
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *--------------------------------------------------------------------------*/

import java.util.List;

public class Main {
    /**
     * Join the given element list with the specified separator
     * 
     * @param elementList
     *            a list of strings to join
     * @param separator
     *            e.g., ",", " ", etc.
     * @return the concatination of the strings in the elementList, separated by the separator
     */
    public static String join(String[] elementList, String separator) {
        StringBuffer b = new StringBuffer();
        for (int i = 0; i < elementList.length - 1; i++) {
            b.append(elementList[i]);
            b.append(separator); // white space
        }
        b.append(elementList[elementList.length - 1]);
        return b.toString();
    }

    public static String join(List elementList, String separator) {
        StringBuffer b = new StringBuffer();
        for (int i = 0; i < elementList.size() - 1; i++) {
            b.append(elementList.get(i));
            b.append(separator); // white space
        }
        b.append(elementList.get(elementList.size() - 1));
        return b.toString();
    }
}

Related

  1. join(String separator, List items)
  2. join(String separator, List parts)
  3. join(String separator, List parts)
  4. join(String separator, List parts)
  5. join(String sign, List src)
  6. join(String[] list, String separator)
  7. join(T delimiter, List list)
  8. join(T element, List otherElements)
  9. joinAndQuote(char joinChar, char quoteChar, List strings)