Here you can find the source of arrayToDelimitedString(Object[] arr, String delim)
Parameter | Description |
---|---|
arr | the array to display |
delim | the delimiter to use (typically a ",") |
public static String arrayToDelimitedString(Object[] arr, String delim)
//package com.java2s; /**//from w ww . ja va2 s . c om * Copyright 2008-2016 Juho Jeong * * 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. */ public class Main { /** The empty {@link String} */ public static final String EMPTY = ""; /** * Convert a {@code String} array into a delimited {@code String} (e.g. CSV). * <p>Useful for {@code toString()} implementations. * * @param arr the array to display * @param delim the delimiter to use (typically a ",") * @return the delimited {@code String} */ public static String arrayToDelimitedString(Object[] arr, String delim) { if (arr == null || arr.length == 0) { return EMPTY; } if (arr.length == 1) { return (arr[0] == null) ? EMPTY : arr[0].toString(); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { if (i > 0) { sb.append(delim); } sb.append(arr[i]); } return sb.toString(); } }