Here you can find the source of join(String sep, Collection
Parameter | Description |
---|---|
sep | the separator |
strs | the collection of string |
public static final String join(String sep, Collection<String> strs)
//package com.java2s; /**/*from w w w.j a v a 2 s . c om*/ * StringUtil.java * * Copyright 2012 Niolex, Inc. * * Niolex licenses this file to you 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.Collection; import java.util.Iterator; public class Main { /** * Join the string array into one single string by the separator. * * @param sep the separator * @param strs the string array * @return the result string */ public static final String join(String sep, String... strs) { return join(strs, sep); } /** * Join the string array into one single string by the separator. * * @param strs the string array * @param sep the separator * @return the result string */ public static final String join(String[] strs, String sep) { if (strs == null || strs.length == 0) { return ""; } StringBuilder sb = new StringBuilder(); sb.append(strs[0]); for (int i = 1; i < strs.length; ++i) { sb.append(sep).append(strs[i]); } return sb.toString(); } /** * Join the string collection into one single string by the separator. * * @param sep the separator * @param strs the collection of string * @return the result string */ public static final String join(String sep, Collection<String> strs) { return join(strs, sep); } /** * Join the string collection into one single string by the separator. * * @param strs the collection of string * @param sep the separator * @return the result string */ public static final String join(Collection<String> strs, String sep) { if (strs == null || strs.size() == 0) { return ""; } StringBuilder sb = new StringBuilder(); Iterator<String> it = strs.iterator(); sb.append(it.next()); while (it.hasNext()) { sb.append(sep).append(it.next()); } return sb.toString(); } }