集册 Java实例教程 数组到格式化逗号分隔字符串

数组到格式化逗号分隔字符串

欢马劈雪     最近更新时间:2020-01-02 10:19:05

546
数组到格式化逗号分隔字符串

/** from 
时代Java公众号**/

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{

        Object[] anArray = new String[]{"1","abc","level",null,"nowjava.com","asdf 123"};

        System.out.println(arrayToFormattedCommaSeparatedString(anArray));

    }

    public static String arrayToFormattedCommaSeparatedString(

            Object[] anArray) {

        StringBuilder result = new StringBuilder();

        int count = 0;


        for (Object value : anArray) {

            if (count > 0) {/*时 代 J a v a 公 众 号 - N o w J a v  a . c o m 提供*/

                result.append(",");

            }

            if (value instanceof String) {

                result.append("'" + value + "'");

            } else 
展开阅读全文