Java Array to Delimited String arrayToDelimitedString(Object[] arr, String delim)

Here you can find the source of arrayToDelimitedString(Object[] arr, String delim)

Description

Convert a String array into a delimited String (e.g.

License

Apache License

Parameter

Parameter Description
arr the array to display
delim the delimiter to use (typically a ",")

Return

the delimited String

Declaration

public static String arrayToDelimitedString(Object[] arr, String delim) 

Method Source Code

//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();
    }
}

Related

  1. arrayToCommaSeparatedString(int[] array)
  2. arrayToCommaString(int[] array)
  3. arrayToCommaString(int[] array)
  4. arrayToDelimitedString(Object[] arr, String delim)
  5. arrayToDelimitedString(Object[] arr, String delim)
  6. arrayToDelimitedString(String[] values, String delimiter)
  7. arrayToDelimitedString(T[] array, String left, String delimiter, String right)
  8. arrayToReadableString(String[] array)
  9. arrayToTabSeparatedString( final String[] values)