取字符串中的数字
String charList = "乳儿班:044个23人 托小班:0个0人 托大班:3个60人 混合班:0个0人"; String integer = ""; //作为数字字符串的预备区 ArrayList<Integer> numList = new ArrayList<>(); for (int i = 0; i < charList.length(); i++) { //遍历字符串中每个字符时,若该字符为数字字符,则存入预备区 if (Character.isDigit(charList.charAt(i))) { integer += charList.charAt(i); //避免字符串最后一位为数字而遗漏收集的情况 if (i == charList.length() - 1) { numList.add(Integer.parseInt(integer)); } }else if (!integer.isEmpty()) { //读取到非数字字符,并且检查预备区是否为空 numList.add(Integer.parseInt(integer)); integer = ""; } } int banji = 0; int people = 0; for (int i = 0; i < numList.size(); i++) { if(i%2 != 0){ banji = numList.get(i) + banji; }else { people = numList.get(i) + people; } } System.out.println(numList); System.out.println(banji); System.out.println(people);
标签:数字,people,int,charList,字符串,integer,numList From: https://www.cnblogs.com/xingmeng63/p/17098682.html