Java Array to String arrayToString(Object[] array, String separator)

Here you can find the source of arrayToString(Object[] array, String separator)

Description

Converts an array of objects to a string.

License

Apache License

Parameter

Parameter Description
array array of objects
separator the separator to put between the object strings

Return

returns a String

Declaration

public static String arrayToString(Object[] array, String separator) 

Method Source Code

//package com.java2s;
/*//from www.  ja  v  a2s . co  m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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.
 */

public class Main {
    /**
     * Converts an array of objects to a string.
     * 
     * @param array array of objects
     * @param separator the separator to put between the object strings
     * @return returns a String
     */
    public static String arrayToString(Object[] array, String separator) {
        if (array == null || array.length < 1)
            return null; // Empty
        if (array.length > 1) { // multi Column Key
            StringBuilder buf = new StringBuilder();
            for (int i = 0; i < array.length; i++) {
                if (i > 0 && separator != null)
                    buf.append(separator);
                buf.append(array[i]);
            }
            return buf.toString();
        }
        // Only one member
        return String.valueOf(array[0]);
    }

    /**
     * Converts a value to a string.
     * If the value is null then the default value is returned.
     * 
     * @param value the value to convert
     * @param defValue default value which to return if value is null
     * @return returns a String representation of the value or null if value is null
     */
    public static String toString(Object value, String defValue) {
        return ((value != null) ? value.toString() : defValue);
    }

    /**
     * Converts a value to a string.
     * If the value is null then null will be returned.
     * 
     * @param value the value to convert
     * @return returns a String representation of the value or null if value is null
     */
    public static String toString(Object value) {
        return toString(value, null);
    }

    /**
     * Converts an array of objects to a string.
     * 
     * @param array array of objects
     * @param defValue default value which to return if array is null
     * @return returns a String representation of the array or the defaultValue if array is null
     */
    public static String toString(Object[] array, String defValue) {
        String s = arrayToString(array, "/");
        return (s != null ? s : defValue);
    }

    /**
     * Converts an array of objects to a string.
     * 
     * @param array array of objects
     * @return returns a String representation of the array or null if the array is null
     */
    public static String toString(Object[] array) {
        return toString(array, null);
    }

    /**
     * Converts a value to a string.
     * if the value is null an empty string is returned.
     * 
     * @param value the value to convert
     * @return returns a String representation of the Object or an empty stringif o is null
     */
    public static String valueOf(Object value) {
        return toString(value, "null");
    }

    /**
     * Converts an objects to a string.
     * 
     * @param array array of objects
     * @return returns a String representation of the array or an empty String if the array is null
     */
    public static String valueOf(Object[] array) {
        return toString(array, "null");
    }
}

Related

  1. arrayToString(Object[] array)
  2. ArrayToString(Object[] array, String conv)
  3. arrayToString(Object[] array, String delim, int maxValues, boolean useBrackets)
  4. arrayToString(Object[] array, String prefix, String suffix, String separator)
  5. arrayToString(Object[] array, String sep)
  6. arrayToString(Object[] array, String separator)
  7. arrayToString(Object[] array, String split)
  8. arrayToString(Object[] ary)
  9. arrayToString(Object[] list)