工具类:提取字符串中的数字串(相邻的数字们分开放在一起)
程序员文章站
2022-05-29 13:53:35
...
工具类描述:
(1)提取字符串中的数字串
(2)各个数字串分开存储
工具代码:
/**
* @author Atimynyc
* @version 1.0
* @title 字符串处理工具
* @since 2019/1/30
*/
public class StringUtils {
//获得数字串工具类
public static List<String> getFullNumFromString(String str){
List<String> resultList = new ArrayList<>();
StringBuilder numBuilder = new StringBuilder();
str = str.replaceAll(" ","");
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 48 && ch <= 57) {
numBuilder.append(ch);
if ( i == str.length() -1) {
resultList.add(numBuilder.toString());
}
} else {
if (!numBuilder.toString().equals("") && numBuilder.length() > 0) {
resultList.add(numBuilder.toString());
numBuilder = new StringBuilder();
}
}
}
return resultList;
}
}
使用示例:
List<String> resultList = StringUtils.getFullNumFromString("500000>=[@[email protected]]>200000");
输出结果:
500000
200000