Java List Join join(String separator, Collection list)

Here you can find the source of join(String separator, Collection list)

Description

join

License

Apache License

Declaration

public static String join(String separator, Collection<?> list) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static String join(String separator, Collection<?> list) {
        return join(separator, null, null, list);
    }//from  w  w w.  ja  v a2 s.  co m

    public static String join(String separator, Collection<?> list, boolean quoted) {
        return join(separator, null, null, list, quoted);
    }

    public static String join(String separator, String prefix, String suffix, Collection<?> list, boolean quoted) {
        StringBuilder sb = new StringBuilder();

        if (null != prefix)
            sb.append(prefix).append(separator);

        Iterator<?> itr = list.iterator();
        if (itr.hasNext()) {
            if (quoted)
                sb.append("\"");
            sb.append(itr.next());
            if (quoted)
                sb.append("\"");
        }
        while (itr.hasNext()) {
            sb.append(separator);
            if (quoted)
                sb.append("\"");
            sb.append(itr.next());
            if (quoted)
                sb.append("\"");
        }

        if (null != suffix)
            sb.append(separator).append(suffix);
        return sb.toString();
    }

    public static String join(String separator, String prefix, String suffix, Collection<?> list) {
        return join(separator, prefix, suffix, list, false);
    }

    public static String join(String separator, String... list) {
        StringBuilder sb = new StringBuilder();

        if (list.length > 0) {
            sb.append(list[0]);
            for (int i = 1; i < list.length; ++i)
                sb.append(separator).append(list[i]);
        }

        return sb.toString();
    }
}

Related

  1. join(String delimiter, List strings)
  2. join(String joiner, List joinees)
  3. join(String joinString, List strings)
  4. join(String joint, List elements)
  5. join(String sep, List a)
  6. join(String separator, Collection inputList)
  7. join(String separator, List items)
  8. join(String separator, List objs)
  9. join(String separator, List elements)