Format an array of objects as a string separated by a delimiter by calling toString on each object - Java java.lang

Java examples for java.lang:String Array

Description

Format an array of objects as a string separated by a delimiter by calling toString on each object

Demo Code

//package com.java2s;

public class Main {
    /**//from   www.j ava  2 s. c  o m
     * Format an array of objects as a string separated by a delimiter by calling toString on each object
     *
     * @param input List to format
     * @param delim delimiter string to insert between elements
     * @return formatted string
     */
    public static String asString(final Object[] input, final String delim) {
        final StringBuffer sb = new StringBuffer();
        for (int i = 0; i < input.length; i++) {
            if (i > 0) {
                sb.append(delim);
            }
            sb.append(input[i].toString());
        }
        return sb.toString();
    }

    /**
     * Format a string array
     *
     * @param input List to format
     * @param delim delimiter string to insert between elements
     * @return formatted string
     */
    public static String asString(final String[] input, final String delim) {
        final StringBuffer sb = new StringBuffer();
        for (int i = 0; i < input.length; i++) {
            if (i > 0) {
                sb.append(delim);
            }
            sb.append(input[i]);
        }
        return sb.toString();
    }
}

Related Tutorials