Java用正则对字符串进行处理并判断是否能转为数字
代码如下所示:
package java_test; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author: gznc_pcc * @date:2018年6月1日 10:50:38 * @version : * */ class Main { public static void main(String[] args) { String lineString = "[\"1\"]"; String line = "[\"on\",\"1\",\"5\",\"8\",\"10\"]"; lineString = line.replaceAll("[\"\\[\\]]", "");//用""替换" [ ] String[] word = lineString.split(","); //以,切割 System.out.println(lineString); for(int i=0;i<word.length;i++){ Pattern pattern = Pattern.compile("[0-9]*"); //正则,匹配数字 Matcher matcher = pattern.matcher(word[i]); if(matcher.matches()){ System.out.println("1:可以转换"); System.out.println(Integer.parseInt(word[i])); } else { System.out.println("2:不能转换"); System.out.println(word[i]); } } } }
总结
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25