Java String Join joinIterable(Iterable element, String separator)

Here you can find the source of joinIterable(Iterable element, String separator)

Description

join Iterable

License

Apache License

Declaration

public static String joinIterable(Iterable<String> element, String separator) 

Method Source Code


//package com.java2s;
/*--------------------------------------------------------------------------
 *  Copyright 2007 utgenome.org//  w  w w .  j  a  va  2  s. c  om
 *
 *  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.ArrayList;
import java.util.List;

public class Main {
    public static String joinIterable(Iterable<String> element, String separator) {
        List<String> list = new ArrayList<String>();
        for (String each : element) {
            list.add(each);
        }
        return join(list, separator);
    }

    /**
     * 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<String> elementList, String separator) {
        if (elementList.isEmpty())
            return "";
        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... xs)
  2. join(T delimiter, List list)
  3. join2(String connector, String... strs)
  4. joinColumns(Iterable columns)
  5. joinedList(T first, T... others)
  6. joinMap(Map map)
  7. joinMap(String keyValueSeperator, String recordSeperator, Map map)
  8. JoinMaps(Map into, Map other)
  9. joinPath(String part1, String part2)