Here you can find the source of join(String[] collection, String delimiter)
Parameter | Description |
---|---|
collection | - collection of items to join. |
delimiter | delimiter to insert between elements of collection. |
public static String join(String[] collection, String delimiter)
//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()); } }