Java Collection Convert toCSVString(Collection theCollection, String theSeparator)

Here you can find the source of toCSVString(Collection theCollection, String theSeparator)

Description

Returns a csv string of the given values.

License

Open Source License

Parameter

Parameter Description
theCollection collection to stringify
theSeparator separator string

Return

csv string

Declaration

public static <T> String toCSVString(Collection<T> theCollection, String theSeparator) 

Method Source Code

//package com.java2s;
/**//from   w  w  w .  j  a  v a2s.  co  m
 * Helpful methods for collections.
 * 
 * ## Legal stuff
 * 
 * Copyright 2014-2014 Ekkart Kleinod <ekleinod@edgesoft.de>
 * 
 * This file is part of edgeUtils.
 * 
 * edgeUtils is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * edgeUtils is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with edgeUtils.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author Ekkart Kleinod
 * @version 0.2
 * @since 0.2
 */

import java.util.Collection;

public class Main {
    /**
     * Returns a csv string of the given values.
     * 
     * @param theCollection collection to stringify
     * @param theSeparator separator string
     * @return csv string
     *    @retval empty if collection is empty or any parameter is null
     * 
     * @version 0.2
     * @since 0.2
     */
    public static <T> String toCSVString(Collection<T> theCollection, String theSeparator) {

        if ((theCollection == null) || (theSeparator == null)) {
            return "";
        }

        StringBuffer sbReturn = new StringBuffer();

        boolean isFurther = false;
        for (T theElement : theCollection) {
            if (isFurther) {
                sbReturn.append(theSeparator);
            }
            if (theElement == null) {
                sbReturn.append(theElement);
            } else {
                sbReturn.append(theElement.toString().trim());
            }
            isFurther = true;
        }

        return sbReturn.toString();
    }
}

Related

  1. toCommaSeparatedString(final Collection items)
  2. toCommaSeparatedValues(Collection list)
  3. toCommaSepared(Collection aListOfString)
  4. toCommaString(Collection c)
  5. toCsvString(Collection collection)
  6. toDelimitedList(Collection values, String delimiter)
  7. toDelimitedString(Collection c, String delimiter)
  8. toDelimitedString(Collection coll, String delim)
  9. toDelimitedString(final Collection coll, final String delim)