java处理数字字符串每5个数字为一组进行拆分,保留逗号。不足5个的也存为一组。

例如: "3,15,23,21,36,32,21,75,25,20,30,23,89,23,92"

返回:

​ "3,15,23,21,36"

​ "32,21,75,25,20"

​ "30,23,89,23,92"

public class Test {
    public static void main(String[] args) {

        String str = "3,15,23,21,36,32,21,75,25,20,30,23,89,23,92";
        String[] strArr = str.split(",");
        int i = 0;
        StringBuilder builder = new StringBuilder();
        for(String item :strArr){

            System.out.println("i:"+i);
            if(i % 5 == 4){
                System.out.println("===========");
                builder.append(item);
                builder.append("-");
                i=-1;
            }else {
                builder.append(item);
                builder.append(",");
            }
            i++;
            System.out.println("i1:"+i);
            System.out.println(builder);

        }
        System.out.println(builder.substring(0,builder.length()-1));
        String[] strArr2 = builder.substring(0,builder.length()-1).split("-");
        for (String item :strArr2){
            System.out.println(item);
        }

    }
}
上一篇:算法训练 递归输出数字三角形 java 题解 449


下一篇:Error attempting to get column 'xxx' from result set. Cause: java.sql.SQLException: 无法转换为