Java Collection Join join(Collection s, String delimiter)

Here you can find the source of join(Collection s, String delimiter)

Description

join

License

Open Source License

Declaration

public static String join(Collection<?> s, String delimiter) 

Method Source Code


//package com.java2s;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static String join(Collection<?> s, String delimiter) {
        return join(s, delimiter, null);
    }/*from www  .  j ava  2s.c  o m*/

    public static String join(Collection<?> s, String delimiter, String prefix) {

        StringBuilder builder = new StringBuilder();
        Iterator<?> iter = s.iterator();

        while (iter.hasNext()) {

            if (!IsNullOrEmpty(prefix)) {
                builder.append(prefix);
            }
            builder.append(iter.next());

            if (!iter.hasNext()) {
                break;
            }
            builder.append(delimiter);
        }
        return builder.toString();
    }

    public static boolean IsNullOrEmpty(String string) {

        return (string == null || string.length() == 0);

    }
}

Related

  1. join(Collection objs, String delim)
  2. join(Collection objs, String delimiter)
  3. join(Collection objs, String sep)
  4. join(Collection s)
  5. join(Collection s, String delimiter)
  6. join(Collection s, String delimiter)
  7. join(Collection strings, String delimiter)
  8. join(Collection strings, String delimiter)
  9. join(Collection strings, String separator)