Java Collection to String listToString(Collection aList, String aDelimiter, boolean quoteEntries)

Here you can find the source of listToString(Collection aList, String aDelimiter, boolean quoteEntries)

Description

Create a String from the given list, where the elements are delimited with the supplied delimiter

License

Apache License

Parameter

Parameter Description
aList The list to process
aDelimiter The delimiter to use
quoteEntries if true, all entries are quoted with a double quote

Return

The elements of the Collection as a String

Declaration

public static String listToString(Collection aList, String aDelimiter, boolean quoteEntries) 

Method Source Code

//package com.java2s;
/*//from w  w w  .j  av  a  2s  .c om
 * StringUtil.java
 *
 * This file is part of SQL Workbench/J, http://www.sql-workbench.net
 *
 * Copyright 2002-2017, Thomas Kellerer
 *
 * Licensed under a modified Apache License, Version 2.0
 * that restricts the use for certain governments.
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at.
 *
 *     http://sql-workbench.net/manual/license.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * To contact the author please send an email to: support@sql-workbench.net
 *
 */

import java.util.Collection;

public class Main {
    public static final String EMPTY_STRING = "";

    /**
     * Create a String from the given Collection, where the elements are delimited
     * with the supplied delimiter
     *
     * @return The elements of the Collection as a String
     * @param aList The list to process
     * @param aDelimiter The delimiter to use
     */
    public static String listToString(Collection aList, char aDelimiter) {
        return listToString(aList, String.valueOf(aDelimiter), false);
    }

    public static String listToString(Collection aList, char aDelimiter, boolean quoteEntries) {
        return listToString(aList, String.valueOf(aDelimiter), quoteEntries);
    }

    /**
     * Create a String from the given list, where the elements are delimited
     * with the supplied delimiter
     *
     * @return The elements of the Collection as a String
     * @param aList The list to process
     * @param aDelimiter The delimiter to use
     * @param quoteEntries if true, all entries are quoted with a double quote
     */
    public static String listToString(Collection aList, String aDelimiter, boolean quoteEntries) {
        return listToString(aList, aDelimiter, quoteEntries, '"');
    }

    public static String listToString(Collection aList, String aDelimiter, boolean quoteEntries, char quote) {
        if (aList == null || aList.isEmpty())
            return EMPTY_STRING;
        int numElements = 0;
        StringBuilder result = new StringBuilder(aList.size() * 50);
        for (Object o : aList) {
            if (o == null)
                continue;
            if (numElements > 0) {
                result.append(aDelimiter);
            }
            if (quoteEntries)
                result.append(quote);
            result.append(o.toString());
            if (quoteEntries)
                result.append(quote);
            numElements++;
        }
        return result.toString();
    }
}

Related

  1. convertString(Collection strings)
  2. convertStringCollectionToString(Collection collectionString, String delemeter)
  3. convertStringCollectionToString(Collection value, String separator)
  4. convertToString(Collection ss)
  5. convertToString(final Collection objects)
  6. listToString(Collection collection)
  7. listToString(Collection l, String delim)
  8. listToString(Collection objects)
  9. listToStringArray(Collection list)