Java String Join join(String delimiter, Collection objects)

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

Description

Joins a collection of objects with given delimiter.

License

Open Source License

Parameter

Parameter Description
delimiter the delimiter to put between every string.
objects a parameter

Return

the joined string

Declaration

public static String join(String delimiter, Collection<?> objects) 

Method Source Code

//package com.java2s;
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by:/*w w  w . j  a  v  a 2  s.  c  om*/
 Department of Geography, University of Bonn
 and
 lat/lon GmbH
    
 This library 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 2.1 of the License, or (at your option)
 any later version.
 This library 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 this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    
 Contact information:
    
 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/
    
 Department of Geography, University of Bonn
 Prof. Dr. Klaus Greve
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/
    
 e-mail: info@deegree.org
 ----------------------------------------------------------------------------*/

import java.util.Arrays;
import java.util.Collection;

public class Main {
    /**
     * Joins a list of strings with given delimiter.
     * 
     * @param delimiter
     *            the delimiter to put between every string.
     * @param strings
     * @return the joined string
     */
    public static String join(String delimiter, String... strings) {
        // call join method for list
        // Arrays.asList is cheap and only creates a wrapper
        return join(delimiter, Arrays.asList(strings));
    }

    /**
     * Joins a list of objects with given delimiter.
     * 
     * @param delimiter
     *            the delimiter to put between every string.
     * @param objects
     * @return the joined string
     */
    public static String join(String delimiter, Object... objects) {
        // call join method for list
        // Arrays.asList is cheap and only creates a wrapper
        return join(delimiter, Arrays.asList(objects));
    }

    /**
     * Joins a collection of objects with given delimiter.
     * 
     * @param delimiter
     *            the delimiter to put between every string.
     * @param objects
     * @return the joined string
     */
    public static String join(String delimiter, Collection<?> objects) {
        StringBuilder sb = new StringBuilder();
        for (Object part : objects) {
            sb.append(part.toString()).append(delimiter);
        }
        if (sb.length() > delimiter.length()) {
            sb.delete(sb.length() - delimiter.length(), sb.length());
        }
        return sb.toString();
    }

    /**
     * Joins a list of <code>int</code>s.
     * 
     * @param delimiter
     * @param values
     * @return the joined string
     */
    public static String join(String delimiter, int[] values) {
        StringBuilder sb = new StringBuilder();
        for (int value : values) {
            sb.append(Integer.toString(value)).append(delimiter);
        }
        if (sb.length() > delimiter.length()) { // remove last delimiter
            sb.delete(sb.length() - delimiter.length(), sb.length());
        }
        return sb.toString();
    }

    /**
     * Joins a list of <code>double</code>s.
     * 
     * @param delimiter
     * @param values
     * @return the joined string
     */
    public static String join(String delimiter, double[] values) {
        StringBuilder sb = new StringBuilder();
        for (double value : values) {
            sb.append(Double.toString(value)).append(delimiter);
        }
        if (sb.length() > delimiter.length()) { // remove last delimiter
            sb.delete(sb.length() - delimiter.length(), sb.length());
        }
        return sb.toString();
    }
}

Related

  1. join(java.util.Collection strings, String delimiter)
  2. join(Map map, String separator)
  3. join(String connector, Object... objects)
  4. join(String delim, List strings)
  5. join(String delim, Object... objects)
  6. join(String delimiter, Collection items)
  7. join(String delimiter, Iterable elements)
  8. join(String delimiter, Iterable strings)
  9. join(String delimiter, Iterable strings)