用正则表达式可以将数字取出来。
public static void main(String[] args) {
String str = "123abc56de";
Pattern p=Pattern.compile("(\\d+)");
Matcher m=p.matcher(str);
Listlist = new LinkedList ();
while(m.find()){
list.add(Integer.decode(m.group()));
}
int count = 0;
for (Integer integer : list) {
count= count+integer;
}
System.out.println(count);
}
public class demo{
public static void main(String args[]) throws Exception{
String str="123abc56de";
String numericArray[]=str.replaceAll("\\D+","-").split("-");
Integer total=sum(numericArray);
System.out.println("total="+total);
}
private static int sum(String ...alg){
int total=0;
for(String x:alg){
System.out.println("x="+x);
total+=Integer.parseInt(x);
}
return total;
}
}