java處理數字字串每5個數字為一組進行拆分,保留逗號。不足5個的也存為一組。
阿新 • • 發佈:2021-10-18
例如: "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); } } }