Java List Join join(List list, String delim)

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

Description

Join each string in the string list list to one string that delimiter by given delimiter delim.

License

Apache License

Parameter

Parameter Description
list a string list, null in the list will be replace with "null"
delim delimiter to delimit each string from the string list

Return

return joined string

Declaration

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

Method Source Code

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

import java.util.*;

public class Main {
    /**/*  ww  w. ja v a2  s  .  co  m*/
     * Join each string in the string list <code>list</code> to one string
     * that delimiter by given delimiter <code>delim</code>.
     * <p>Examples:
     * <blockquote><pre>
     * List strList = new ArrayList();
     * strList.add("hello");
     * strList.add("world");
     * strList.add(null);
     * StringUtil.join(strList, " ");
     * 
     * return "hello world null"
     * </pre></blockquote>
     * @param list a string list, null in the list will be replace with "null"
     * @param delim delimiter to delimit each string from the string list
     * @return return joined string
     */
    public static String join(List<String> list, String delim) {
        if ((list == null) || (list.size() < 1)) {
            return null;
        }

        StringBuffer buf = new StringBuffer();
        Iterator<String> i = list.iterator();

        while (i.hasNext()) {
            buf.append((String) i.next());

            if (i.hasNext()) {
                buf.append(delim);
            }
        }

        return buf.toString();
    }

    /**
     * Get string value of given Object <code>param</code>.
     * This method will invoke toString() method on given object if not null.
     * @param param object instance
     * @return string represent given object if it's not null, otherwise return null.
     */
    public static String toString(Object param) {
        if (param == null) {
            return null;
        }

        return param.toString().trim();
    }
}

Related

  1. join(List list)
  2. join(List list)
  3. join(List list)
  4. join(List list)
  5. join(List list, String conjunction)
  6. join(List list, String delim)
  7. join(List list, String delimiter)
  8. join(List list, String delimiter)
  9. join(List list, String delimiter)