array To Comma Separated String - Java Collection Framework

Java examples for Collection Framework:Array Join

Description

array To Comma Separated String

Demo Code


import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

public class Main{
    public static void main(String[] argv) throws Exception{
        String[] stringArray = new String[]{"1","abc","level",null,"java2s.com","asdf 123"};
        System.out.println(arrayToCommaSeparatedString(stringArray));
    }//  w w w  . j  a v a  2s  .  c  o  m
    public static String arrayToCommaSeparatedString(String[] stringArray) {
        StringBuilder result = new StringBuilder();
        int count = 0;

        for (String value : stringArray) {
            if (count > 0) {
                result.append(",");
            }
            result.append(value);
            count++;
        }
        return result.toString();
    }
}

Related Tutorials