Java - Write code to convert string array To String with specified separator

Requirements

Write code to convert string array To String with specified separator

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String[] array = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        String separator = ",";
        System.out.println(convertToString(array, separator));
    }/*w w w . jav  a2  s .  c o m*/

    public static String convertToString(String[] array, String separator) {
        String elementSeparator = "";
        StringBuilder builder = new StringBuilder();
        for (String s : array) {
            builder.append(elementSeparator).append(s);
            elementSeparator = separator;
        }
        return builder.toString();
    }
}

Related Exercise