Java List Join join(List list, String separator)

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

Description

join

License

Open Source License

Declaration

public static String join(List<String> list, String separator) 

Method Source Code

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

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static String join(List<String> list, String separator) {
        if (list == null) {
            return null;
        }/*w ww.  j  a va 2s  . co m*/
        if (list.isEmpty()) {
            return "";
        }
        if (separator == null) { // ???
            separator = "";
        }

        StringBuilder sb = new StringBuilder();
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            sb.append(it.next());
            if (it.hasNext()) {
                sb.append(separator);
            }
        }

        return sb.toString();
    }

    public static String join(String s1, String s2, String separator) {
        List<String> list = new ArrayList<String>();
        list.add(s1);
        list.add(s2);
        return join(list, separator);
    }

    public static String join(String s1, String s2, String s3, String separator) {
        List<String> list = new ArrayList<String>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        return join(list, separator);
    }
}

Related

  1. join(List list, String delimiter)
  2. join(List list, String delimiter)
  3. join(List list, String delimiter)
  4. join(List list, String sep)
  5. join(List list, String separator)
  6. join(List list1, List list2)
  7. join(List list1, List list2)
  8. join(List lst, int start, int end)
  9. join(List lst, String separator)