优雅的字符串拼接去掉逗号方式

阅读 75

2022-01-25

talk is cheep!show you the code!!

public static void main(String[] args) {
        List<String> appends = new ArrayList<>();
        appends.add("aaa");
        appends.add("bbb");
        appends.add("ccc");
        StringBuilder content = new StringBuilder();
        int size = appends.size();
        if (!CollectionUtils.isEmpty(appends)) {
            for (int i = 0; i < size; i++) {
                content.append(appends.get(i));
                // 最后一个不加逗号
                if (size - 1 == i) {
                    break;
                }
                content.append(",");
            }
        }
        System.out.println(content);
    }

精彩评论(0)

0 0 举报