Java Collection Join join(Collection objs, String sep)

Here you can find the source of join(Collection objs, String sep)

Description

Join a collection with a separator string

License

Open Source License

Declaration

public static String join(Collection<?> objs, String sep) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**//from   ww w .  j  a  va2 s.  c  o m
     * Join a collection with a separator string
     */
    public static String join(Collection<?> objs, String sep) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;

        for (Object obj : objs) {
            if (!first) {
                sb.append(sep);
            }
            sb.append(obj.toString());
            first = false;
        }

        return sb.toString();
    }

    /**
     * Join an array with a separator string
     */
    public static <T> String join(T[] array, String sep) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;

        for (T e : array) {
            if (!first) {
                sb.append(sep);
            }
            sb.append(e.toString());
            first = false;
        }

        return sb.toString();
    }
}

Related

  1. join(Collection objects, String token, StringBuilder buf)
  2. join(Collection objectsToJoin, String separator)
  3. join(Collection objs, String delim)
  4. join(Collection objs, String delim)
  5. join(Collection objs, String delimiter)
  6. join(Collection s)
  7. join(Collection s, String delimiter)
  8. join(Collection s, String delimiter)
  9. join(Collection s, String delimiter)