【发布时间】:2018-10-16 09:13:57
【问题描述】:
我有一个字符串“AB12TRHW4TR6HH58”,我需要拆分这个字符串,每当我找到一个数字时,我都需要执行所有的加法。但是,如果有连续的数字,那么我需要将它作为一个整数。 例如,在上面的字符串中,加法应该像 12+4+6+58 等等。
我在下面编写了单独添加所有数字但不能取整数的代码。你能帮忙吗?
public class TestClass {
public static void main(String[] args) {
String str = "AB12TRHW4TR6HH58";
int len = str.length();
String[] st1 = str.split("");
int temp1=0;
for(int i=0;i<=len-1;i++){
if(st1[i].matches("[0-9]")){
int temp = Integer.parseInt(st1[i]);
temp1 = temp+temp1;
}
}
System.out.println(temp1);
}
}
【问题讨论】:
-
用字母作为分隔符分割你的字符串,你会得到一个十进制数的数组。