Java Collection Join join(String[] collection, String delimiter)

Here you can find the source of join(String[] collection, String delimiter)

Description

Joins the items in collection with a delimiter.

License

Apache License

Parameter

Parameter Description
collection - collection of items to join.
delimiter delimiter to insert between elements of collection.

Return

string with collection elements separated by delimiter. There is no trailing delimiter in the string.

Declaration

public static String join(String[] collection, String delimiter) 

Method Source Code

//package com.java2s;
/*/* w ww  .  ja va 2  s .c om*/
 Copyright 2009-2010 Igor Polevoy 

 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. 
 */

import java.util.*;

public class Main {
    /**
     * Joins the items in collection with a delimiter.
     *
     * @param collection - collection of items to join.
     * @param delimiter delimiter to insert between elements of collection.
     * @return string with collection elements separated by delimiter. There is no trailing delimiter in the string.
     */
    public static String join(String[] collection, String delimiter) {
        return join(Arrays.asList(collection), delimiter);
    }

    /**
     * Joins the items in collection with a delimiter.
     *
     * @param collection - collection of items to join.
     * @param delimiter delimiter to insert between elements of collection.
     * @return string with collection elements separated by delimiter. There is no trailing delimiter in the string.
     */
    public static String join(Collection collection, String delimiter) {
        if (collection.size() == 0)
            return "";

        String tmp = "";
        for (Object o : collection) {
            tmp += o + delimiter;
        }
        return tmp.substring(0, tmp.length() - delimiter.length());
    }
}

Related

  1. join(String separator, Collection parts)
  2. join(String separator, Collection strings)
  3. join(String separator, Collection objects)
  4. join(String separator, Collection values)
  5. join(String token, Collection strings)
  6. join(String[] collection, String separator)
  7. join(StringBuilder builder, Collection strs)
  8. join2(Collection list, String delimiter)
  9. joinAndDelimit(final Collection strings, final String sep, final String delim)