String findMemory ="=320";//=320
String regEx="[^0-9]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(findMemory);
String trim = m.replaceAll("").trim();
int a = Integer.parseInt(trim == null ? "0":trim);//转换成int类型,且如果为null 定义成0
System.out.println(trim);//"320"
System.out.println(a+a);//640
使用正则表达式获取字符串中的数字
于 2022-03-31 09:32:19 首次发布
本文介绍了一种使用Java正则表达式去除字符串中非数字字符的方法,并演示了如何将处理后的字符串转换为整数类型。通过实例代码展示了从初始化字符串到最终输出整数运算结果的全过程。
1351

被折叠的 条评论
为什么被折叠?



