Java Array Join join(Object[] parts, String delim)

Here you can find the source of join(Object[] parts, String delim)

Description

Join a string array into a single string.

License

Open Source License

Parameter

Parameter Description
parts The string array to join.
delim The delimeter to join strings at.

Declaration

public static String join(Object[] parts, String delim) 

Method Source Code

//package com.java2s;
/*//from   w w w  .ja  va2s . com
com.rivescript.RiveScript - The Official Java RiveScript Interpreter
Copyright (C) 2010  Noah Petherbridge
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
This program 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 General Public License for more details.
    
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * Join a string array into a single string.
     *
     * @param parts The string array to join.
     * @param delim The delimeter to join strings at.
     */
    public static String join(Object[] parts, String delim) {
        StringBuilder buff = new StringBuilder();
        for (int i = 0; i < parts.length; i++) {
            buff.append(parts[i].toString());
            if (i < parts.length - 1) {
                buff.append(delim);
            }
        }
        return buff.toString();
    }

    public static String join(Collection<String> collection, String delim) {
        StringBuilder buff = new StringBuilder();

        for (Iterator<String> iter = collection.iterator(); iter.hasNext();) {
            String item = (String) iter.next();
            buff.append(item);
            if (iter.hasNext()) {
                buff.append(delim);
            }
        }
        return buff.toString();
    }
}

Related

  1. join(Object[] array, String separator)
  2. join(Object[] array, String seperator)
  3. join(Object[] elements, CharSequence separator)
  4. join(Object[] elements, String glue)
  5. join(Object[] objects)
  6. join(Object[] strings, String delimiter)
  7. join(Object[] strings, String spliter)
  8. join(Object[] tokens, String delimiter)
  9. join(Object[] values, String join)