输入一个字符串,内有数字和非数字字符,如:a123b4c。连续的数字为一个整体,依次放入list中,然后输出123,4 编写程序统计其共有多少个整数,并输出。
效果图:
遍历要处理的字符串,判断每个子字符是不是数字,是则继续拼接,不是这把此次得到的数字存进list,然后继续遍历
代码:
public static void jieQuZiFuChuanZhengXin(String str) {
String ints = "";
List<Integer> list = new ArrayList<Integer>();
for (String s : str.split("")) {
if (s.length() == 0) {
continue;
}
int chr = s.charAt(0);
if (chr < 48 || chr > 57) {
if (ints.length() > 0) {
list.add(Integer.parseInt(ints));
ints = "";
}
} else {
ints += s;
}
}
System.out.println(list);
}