项目中需要拿字符串中的第一个数字,找到了如下方法,特此记录String wind = "3级";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(wind);
m.find();
int currentWind =Integer.parseInt(m.group());
输出currentWind为数字3
当我测试字符串 wind=“大于3级小于6级”拿到的数字是3.能满足我的需求
本文介绍了一种使用正则表达式从字符串中提取首个数字的方法。通过Pattern和Matcher类的配合,能够准确找到并转换字符串中的第一个数字。
项目中需要拿字符串中的第一个数字,找到了如下方法,特此记录String wind = "3级";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(wind);
m.find();
int currentWind =Integer.parseInt(m.group());
输出currentWind为数字3
当我测试字符串 wind=“大于3级小于6级”拿到的数字是3.能满足我的需求
1万+

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