Java Collection Join join(final char _separator, final boolean _quotes, final Collection _list, final String _emptyString)

Here you can find the source of join(final char _separator, final boolean _quotes, final Collection _list, final String _emptyString)

Description

A list of string is joined to one string.

License

Apache License

Parameter

Parameter Description
_separator separator between two list items
_quotes surround the elements of the string with quotes
_list list of strings
_emptyString string which is written if the list is empty (or <code>null</code> if no string for empty list is written)

Return

joined string of the list items

Declaration

public static String join(final char _separator, final boolean _quotes, final Collection<String> _list,
        final String _emptyString) 

Method Source Code

//package com.java2s;
/*//from   www .j av a  2 s.  co  m
 * Copyright 2008-2011 The MxUpdate Team
 *
 * Licensed 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.
 *
 * Revision:        $Rev$
 * Last Changed:    $Date$
 * Last Changed By: $Author$
 */

import java.util.Collection;

public class Main {
    /**
     * A list of string is joined to one string. Between two string the given
     * separator is set. If quotes parameter is defined each element of the
     * list is surrounded with quotes.
     *
     * @param _separator    separator between two list items
     * @param _quotes       surround the elements of the string with quotes
     * @param _list         list of strings
     * @param _emptyString  string which is written if the list is empty (or
     *                      <code>null</code> if no string for empty list is
     *                      written)
     * @return joined string of the list items
     */
    public static String join(final char _separator, final boolean _quotes, final Collection<String> _list,
            final String _emptyString) {
        final StringBuilder ret = new StringBuilder();

        boolean first = true;
        if (_list.isEmpty()) {
            if (_emptyString != null) {
                ret.append(_emptyString);
            }
        } else {
            for (final String elem : _list) {
                if (!first) {
                    ret.append(_separator);
                } else {
                    first = false;
                }
                if (_quotes) {
                    ret.append('\"');
                }
                ret.append(elem);
                if (_quotes) {
                    ret.append('\"');
                }
            }
        }
        return ret.toString();
    }
}

Related

  1. join(Collection list, final String separator)
  2. join(Collection list, String delimiter)
  3. join(Collection s, String delimiter)
  4. join(Collection things, String delim)
  5. join(Collection values, String join)
  6. join(final Collection collection, String delimiter)
  7. join(final Collection e, final String s)
  8. join(final Collection items, final String glue)
  9. join(final Collection coll, final String delimiter)