Java Collection Join join(Collection objects, String delim)

Here you can find the source of join(Collection objects, String delim)

Description

Join a set of strings into one string, putting the specified delimiter between adjacent strings.

License

BSD License

Parameter

Parameter Description
objects A collection the items to be joined. This collection can contain objects of any type; each object's <tt>toString()</tt> method is called to produce the string to be joined.
delim the delimiter string

Return

the joined string, or "" if the collection is empty.

Declaration

public static String join(Collection<? extends Object> objects, String delim) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------------------*\
  $Id: a2e18c1fd38fa21f86084fbdb2c0045cec86bd20 $
  ---------------------------------------------------------------------------
  This software is released under a BSD-style license:
    //w  w  w. j a  v  a  2 s  .  c o  m
  Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.
    
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
    
  1.  Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
    
  2.  The end-user documentation included with the redistribution, if any,
  must include the following acknowlegement:
    
"This product includes software developed by Brian M. Clapper
(bmc@clapper.org, http://www.clapper.org/bmc/). That software is
copyright (c) 2004-2007 Brian M. Clapper."
    
  Alternately, this acknowlegement may appear in the software itself,
  if wherever such third-party acknowlegements normally appear.
    
  3.  Neither the names "clapper.org", "clapper.org Java Utility Library",
  nor any of the names of the project contributors may be used to
  endorse or promote products derived from this software without prior
  written permission. For written permission, please contact
  bmc@clapper.org.
    
  4.  Products derived from this software may not be called "clapper.org
  Java Utility Library", nor may "clapper.org" appear in their names
  without prior written permission of Brian M. Clapper.
    
  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  NO EVENT SHALL BRIAN M. CLAPPER BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\*---------------------------------------------------------------------------*/

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

public class Main {
    /**
     * Join a set of strings into one string, putting the specified delimiter
     * between adjacent strings.
     *
     * @param strings  the strings to be joined
     * @param delim    the delimiter string
     *
     * @return the joined string, or "" if the array is empty.
     *
     * @see #split(String,String)
     * @see #join(String[],char)
     */
    public static String join(String[] strings, String delim) {
        StringBuilder result = new StringBuilder();
        String sep = "";

        for (int i = 0; i < strings.length; i++) {
            result.append(sep);
            result.append(strings[i]);
            sep = delim;
        }

        return result.toString();
    }

    /**
     * Join a set of strings into one string, putting the specified delimiter
     * between adjacent strings. This version of <tt>join()</tt> supports the
     * new Java variable argument syntax.
     *
     * @param delim    the delimiter string
     * @param strings  the strings to be joined
     *
     * @return the joined string, or "" if the array is empty.
     *
     * @see #split(String,char)
     * @see #join(String[],String)
     */
    public static String join(String delim, String... strings) {
        return join(strings, delim);
    }

    /**
     * Join an array of strings into one string, putting the specified
     * delimiter between adjacent strings.
     *
     * @param strings  the strings to be joined
     * @param delim    the delimiter character
     *
     * @return the joined string, or "" if the array is empty.
     *
     * @see #split(String,char)
     * @see #join(String[],String)
     */
    public static String join(String[] strings, char delim) {
        return join(strings, "" + delim);
    }

    /**
     * Join an array of strings into one string, putting the specified
     * delimiter between adjacent strings, starting at a specified index.
     *
     * @param strings  the strings to be joined
     * @param start    starting index
     * @param end      one past the ending index
     * @param delim    the delimiter character
     *
     * @return the joined string, or "" if the array is empty.
     *
     * @throws ArrayIndexOutOfBoundsException bad value for <tt>start</tt>
     *                                        or <tt>end</tt>
     *
     * @see #split(String,char)
     * @see #join(String[],String)
     */
    public static String join(String[] strings, int start, int end, char delim) {
        return join(strings, start, end, "" + delim);
    }

    /**
     * Join an array of strings into one string, putting the specified
     * delimiter between adjacent strings, starting at a specified index.
     *
     * @param strings  the strings to be joined
     * @param start    starting index
     * @param end      one past the ending index
     * @param delim    the delimiter string
     *
     * @return the joined string, or "" if the array is empty.
     *
     * @throws ArrayIndexOutOfBoundsException bad value for <tt>start</tt>
     *                                        or <tt>end</tt>
     *
     * @see #split(String,char)
     * @see #join(String[],String)
     */
    public static String join(String[] strings, int start, int end, String delim) {
        StringBuilder result = new StringBuilder();
        String sep = "";

        while (start < end) {
            result.append(sep);
            result.append(strings[start]);
            sep = delim;
            start++;
        }

        return result.toString();
    }

    /**
     * Join a set of strings into one string, putting the specified delimiter
     * between adjacent strings. This version of <tt>join()</tt> supports the
     * new Java variable argument syntax.
     *
     * @param delim    the delimiter character
     * @param strings  the strings to be joined
     *
     * @return the joined string, or "" if the array is empty.
     *
     * @see #split(String,char)
     * @see #join(String[],String)
     */
    public static String join(char delim, String... strings) {
        return join(strings, "" + delim);
    }

    /**
     * Join a set of strings into one string, putting the specified delimiter
     * between adjacent strings.
     *
     * @param objects  A collection the items to be joined. This collection
     *                 can contain objects of any type; each object's
     *                 <tt>toString()</tt> method is called to produce the
     *                 string to be joined.
     * @param delim    the delimiter string
     *
     * @return the joined string, or "" if the collection is empty.
     *
     * @see #split(String,String,Collection)
     * @see #join(Collection,char)
     */
    public static String join(Collection<? extends Object> objects, String delim) {
        String result = "";

        if (objects.size() > 0) {
            String[] array;
            int i;
            Iterator it;

            i = 0;
            for (it = objects.iterator(); it.hasNext();) {
                Object o = it.next();
                if (o == null)
                    continue;

                i++;
            }

            array = new String[i];
            i = 0;
            for (it = objects.iterator(); it.hasNext();) {
                Object o = it.next();
                if (o == null)
                    continue;

                array[i++] = o.toString();
            }

            result = join(array, delim);
        }

        return result;
    }

    /**
     * Join a set of strings into one string, putting the specified delimiter
     * between adjacent strings.
     *
     * @param objects  A collection the items to be joined. This collection
     *                 can contain objects of any type; each object's
     *                 <tt>toString()</tt> method is called to produce the
     *                 string to be joined.
     * @param delim    the delimiter character
     *
     * @return the joined string, or "" if the collection is empty.
     *
     * @see #split(String,char,Collection)
     * @see #join(Collection,String)
     */
    public static String join(Collection<? extends Object> objects, char delim) {
        return join(objects, "" + delim);
    }
}

Related

  1. join(Collection c, String joinWith)
  2. join(Collection collection, String joinString)
  3. join(Collection collection, String separator)
  4. join(Collection elements, String separator)
  5. join(Collection list, String separator)
  6. join(Collection strings, String separator)
  7. join(Collection strs, String separator)
  8. join(Collection c, String delim)
  9. join(Collection c, String delimiter)