日常码代码中遇到字符串的操作是很平常的事,如果每次都重复写相同的语句变得很多余,增加冗余工作量,封装成工具类方便使用。
1.创建StringUtils类,用正则表达式表示数字;
public class StringUtils {
public static final String NUMBER_REGEX = "([0-9]\\d*\\.?\\d*)||(0\\.\\d*[1-9])";
}2.判断字符串是否为数字,返回布尔值;
//判断字符串是否是数字
public static boolean isNumber(String s) {
return s.matches(NUMBER_REGEX);
}
3.判断字符串是否为数字,返回布尔值;
//判断字符串是否为空
public static boolean isEmpty(String cs) {
return cs == null || cs.length() == 0;
}4.获取字符串中的数字;
//获取字符串中的数字
public static String getNumbers(String content) {
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(content)版权声明:本文为m0_58596092原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。