Java - Write code to convert String array to Comma Separated string

Requirements

Write code to convert String array to Comma Separated string

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String[] str = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        System.out.println(toCommaSeparated(str));
    }// w w  w  .  j a  v a  2s  . co m

    public static String toCommaSeparated(String[] str) {
        String result = "";

        if (str.length > 0) {
            result = str[0];
            for (int i = 1; i < str.length; i++) {
                result += "," + str[i];
            }
        }

        return result;
    }
}

Related Exercise