Java Collection to String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)

Here you can find the source of collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)

Description

Convenience method to return a Collection as a delimited (e.g.

License

Apache License

Parameter

Parameter Description
coll the Collection to display
delim the delimiter to use (probably a ",")
prefix the String to start each element with
suffix the String to end each element with

Return

the delimited String Borrowed from Spring, under the ASL2.0 license.

Declaration

public static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix) 

Method Source Code

//package com.java2s;
/*/*  ww w  .  j a  v  a 2s .  c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * 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.
 */

import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**
     * Convenience method to return a Collection as a delimited (e.g. CSV)
     * String. E.g. useful for <code>toString()</code> implementations.
     * @param coll the Collection to display
     * @param delim the delimiter to use (probably a ",")
     * @param prefix the String to start each element with
     * @param suffix the String to end each element with
     * @return the delimited String
     * 
     * Borrowed from Spring, under the ASL2.0 license.
     */
    public static String collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix) {
        if (coll == null || coll.isEmpty()) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Iterator it = coll.iterator();
        while (it.hasNext()) {
            sb.append(prefix).append(it.next()).append(suffix);
            if (it.hasNext()) {
                sb.append(delim);
            }
        }
        return sb.toString();
    }

    /**
     * Convenience method to return a Collection as a delimited (e.g. CSV)
     * String. E.g. useful for <code>toString()</code> implementations.
     * @param coll the Collection to display
     * @param delim the delimiter to use (probably a ",")
     * @return the delimited String
     * 
     * Borrowed from Spring, under the ASL2.0 license.
     */
    public static String collectionToDelimitedString(Collection coll, String delim) {
        return collectionToDelimitedString(coll, delim, "", "");
    }

    /**
     * <p>Checks if a String is empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isEmpty(null)      = true
     * StringUtils.isEmpty("")        = true
     * StringUtils.isEmpty(" ")       = false
     * StringUtils.isEmpty("bob")     = false
     * StringUtils.isEmpty("  bob  ") = false
     * </pre>
     *
     * <p>NOTE: This method changed in Lang version 2.0.
     * It no longer trims the String.
     * That functionality is available in isBlank().</p>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is empty or null
     */
    public static boolean isEmpty(final CharSequence str) {
        if (str == null || str.length() == 0) {
            return true;
        }

        for (int i = 0, length = str.length(); i < length; i++) {
            if (str.charAt(i) != ' ') {
                return false;
            }
        }

        return true;
    }
}

Related

  1. collectionToCommaString(Collection bundleSelection)
  2. collectionToCSString(Collection col)
  3. collectionToDelimitedString( Iterable iterable)
  4. collectionToDelimitedString(Collection c, String delim)
  5. collectionToDelimitedString(Collection coll, String delim)
  6. collectionToDelimitedString(Collection coll, String delim, String prefix, String suffix)
  7. collectionToDelimitedString(Collection col, String del)
  8. collectionToDelimitedString(Collection coll, String delim)
  9. collectionToDelimitedString(Collection coll, String delim)