Java Iterable Join join(String separator, Iterable args)

Here you can find the source of join(String separator, Iterable args)

Description

join

License

Mozilla Public License

Parameter

Parameter Description
separator character to put between arguments
args items to string together
T type of items

Return

"" for empty list, otherwise arg1 + separator + arg2 + separator + ...

Declaration

public static <T> String join(String separator, Iterable<T> args) 

Method Source Code

//package com.java2s;
//     The contents of this file are subject to the Mozilla Public License

import java.util.*;

public class Main {
    /**//from   w  ww.j a  v a 2 s  .  c  o m
     *
     * @param separator character to put between arguments
     * @param args items to string together
     * @param <T> type of items
     * @return "" for empty array, otherwise arg1 + separator + arg2 + separator + ...
     */
    public static <T> String join(String separator, T... args) {
        return join(separator, Arrays.asList(args));
    }

    /**
     *
     * @param separator character to put between arguments
     * @param args items to string together
     * @param <T> type of items
     * @return "" for empty list, otherwise arg1 + separator + arg2 + separator + ...
     */
    public static <T> String join(String separator, Iterable<T> args) {
        StringBuilder builder = new StringBuilder();

        for (T project : args) {
            builder.append(project);
            builder.append(separator);
        }

        if (builder.length() > 0) {
            return builder.substring(0, builder.length() - 1);
        } else {
            return "";
        }
    }
}

Related

  1. join(String glue, Iterable pieces)
  2. join(String glue, Iterable tokens)
  3. join(String sep, Iterable strings)
  4. join(String separator, Iterable elements)
  5. join(String separator, Iterable objects)
  6. join(String seperator, Iterator objects)
  7. join(String seprator, Iterable coll)
  8. join(StringBuilder buf, Iterable values, String separator)
  9. joinIterableOnComma(Iterable iterable)