Here you can find the source of join(Object[] array, String separator)
Joins the elements of the provided array into a single String containing the provided list of elements.
No delimiter is added before or after the list.
Parameter | Description |
---|---|
array | the array of values to join together, may be null |
separator | the separator character to use, null treated as "" |
null
if null array input
public static String join(Object[] array, String separator)
//package com.java2s; /*//from w w w .j ava2s. c om * Copyright 2014 Red Hat, Inc. and/or its affiliates. * * 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.Collection; import java.util.Iterator; public class Main { /** * <p>Joins the elements of the provided array into a single String * containing the provided list of elements.</p> * * <p>No delimiter is added before or after the list. * A <code>null</code> separator is the same as an empty String (""). * Null objects or empty strings within the array are represented by * empty strings.</p> * * <pre> * StringUtils.join(null, *) = null * StringUtils.join([], *) = "" * StringUtils.join([null], *) = "" * StringUtils.join(["a", "b", "c"], "--") = "a--b--c" * StringUtils.join(["a", "b", "c"], null) = "abc" * StringUtils.join(["a", "b", "c"], "") = "abc" * StringUtils.join([null, "", "a"], ',') = ",,a" * </pre> * * @param array the array of values to join together, may be null * @param separator the separator character to use, null treated as "" * @return the joined String, <code>null</code> if null array input */ public static String join(Object[] array, String separator) { if (array == null) { return null; } StringBuilder builder = new StringBuilder(); for (int i = 0; i < array.length; i++) { Object o = array[i]; if (i > 0) { builder.append(separator); } builder.append(o); } return builder.toString(); } /** * Joins a collection of string with a given delimiter. * * @param strings The collection of strings to join. * @param delimiter The delimiter to use to join them. * @return The string built by joining the string with the delimiter. */ public static String join(Collection strings, String delimiter) { StringBuilder builder = new StringBuilder(); Iterator<String> iter = strings.iterator(); while (iter.hasNext()) { builder.append(iter.next()); if (!iter.hasNext()) { break; } builder.append(delimiter); } return builder.toString(); } }