Java List Trim toStringList(List list, String delimiter, boolean isTrim, boolean isRemoveEmpty)

Here you can find the source of toStringList(List list, String delimiter, boolean isTrim, boolean isRemoveEmpty)

Description

Converts a List of object to a string list separated by specified delimiter.
(Do trim for each string element if isTrim is true).

License

LGPL

Parameter

Parameter Description
list a list of object
delimiter specified delimiter
isTrim whether do trim for each string element
isRemoveEmpty whether remove empty string element

Return

result string list

Declaration

public static String toStringList(List<?> list, String delimiter, boolean isTrim, boolean isRemoveEmpty) 

Method Source Code

//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();
    }
}

Related

  1. splitAsList(String source, char delimiter, boolean trim)
  2. stringToListTrim(String str, char delimiter)
  3. toDelimitedString(List values, String delimiter, boolean trimValues)
  4. tokenizeAndTrimToList(String value, String delimiter)
  5. toList(final String line, final boolean trimSpaces)
  6. trim(final List row)
  7. trim(List data, boolean removeEmptyLines)
  8. trim(List strs)
  9. trimElements(List list)