Here you can find the source of join(List
list
to one string that delimiter by given delimiter delim
.
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 |
public static String join(List<String> list, String delim)
//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(); } }