package com.sj.zy;
public class Zy02 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Promise pe = new Promise();
String str = "abc123hhd998";
String replaceAll = pe.Replace(str);
char[] ch = replaceAll.toCharArray();
int sum =0;
for (int i = 0; i < ch.length; i++) {//值为Ascii码值
int j = ch[i]-'0';//如果是char类型的单个字母, 可以直接把char类型变量减去char型的0然后赋值给int类型。
sum= sum+j;
System.out.println(j);//输出j并查看是什么
}
System.out.println(sum);
}
}
类:
package com.sj.zy;
public class Promise {
public String Replace(String str) {
String regex = "[^0-9]+";
String result = str.replaceAll(regex, "");
return result;
}
}