Here you can find the source of toStringList(List> list, String delimiter, boolean isTrim, boolean isRemoveEmpty)
Parameter | Description |
---|---|
list | a list of object |
delimiter | specified delimiter |
isTrim | whether do trim for each string element |
isRemoveEmpty | whether remove empty string element |
public static String toStringList(List<?> list, String delimiter, boolean isTrim, boolean isRemoveEmpty)
//package com.java2s; //License from project: LGPL import java.util.List; public class Main { /**//w w w.ja va 2 s .c om * Converts a List of object to a string list separated by specified delimiter.<br> * * (Do trim for each string element). <br> * * (Remove empty string element). <br> * * @param list * a list of object * @param delimiter * specified delimiter * @return result string list */ public static String toStringList(List<?> list, String delimiter) { return toStringList(list, delimiter, true); } /** * Converts a List of object to a string list separated by specified delimiter.<br> * * (Do trim for each string element if isTrim is true). <br> * * (Remove empty string element). <br> * * @param list * a list of object * @param delimiter * specified delimiter * @param isTrim * whether do trim for each string element * @return result string list */ public static String toStringList(List<?> list, String delimiter, boolean isTrim) { return toStringList(list, delimiter, true, true); } /** * Converts a List of object to a string list separated by specified delimiter.<br> * * (Do trim for each string element if isTrim is true). <br> * * (Remove empty string element if isRemoveEmpty is true).<br> * * @param list * a list of object * @param delimiter * specified delimiter * @param isTrim * whether do trim for each string element * @param isRemoveEmpty * whether remove empty string element * @return result string list */ public static String toStringList(List<?> list, String delimiter, boolean isTrim, boolean isRemoveEmpty) { StringBuffer sb = new StringBuffer(); boolean firstElem = true; for (Object o : list) { String s = (o == null) ? "" : o.toString(); if (isTrim) { s = s.trim(); } if (isRemoveEmpty && s.equals("")) { continue; } if (firstElem) { sb.append(s); firstElem = false; } else { sb.append(delimiter + s); } } return sb.toString(); } /** * Converts a List of object to a string list separated by specified delimiter.<br> * * (Do trim for each string element if isTrim is true). <br> * * (Remove empty string element if isRemoveEmpty is true).<br> * * @param list * a list of object * @param delimiter * specified delimiter * @param isTrim * whether do trim for each string element * @param isRemoveEmpty * whether remove empty string element * @param leftEnclosedBy * string to be enclosed on the left * @param rightEnclosedBy * string to be enclosed on the right * @return result string list */ public static String toStringList(List<?> list, String delimiter, boolean isTrim, boolean isRemoveEmpty, String leftEnclosedBy, String rightEnclosedBy) { StringBuffer sb = new StringBuffer(); boolean firstElem = true; for (Object o : list) { String s = (o == null) ? "" : o.toString(); if (isTrim) { s = s.trim(); } if (isRemoveEmpty && s.equals("")) { continue; } s = leftEnclosedBy + s + rightEnclosedBy; if (firstElem) { sb.append(s); firstElem = false; } else { sb.append(delimiter + s); } } return sb.toString(); } }