JAVA 从字符串中提取数字
1 public void test() { 2 2 String a = "hada16@54$1wd!6a41$da1dw5d6&a1d&a41d*w6d14a#d61d"; 3 3 /* 4 4 * 正则表达式(一般用这个) 5 5 * 6 6 * */ 7 7 String regEx = "[^0-9]"; 8 8 Pattern p = Pattern.compile(regEx); 9 9 Matcher m = p.matcher(a); 10 10 String str = m.replaceAll("").trim(); 11 11 System.out.println(str);//1654164115614161461 12 12 /* 13 13 * 14 14 * 用字符串处理 15 15 * */ 16 16 a = a.trim(); 17 17 String b = ""; 18 18 if(a != null && !"".equals(a)) { 19 19 for (int i = 0; i < a.length(); i++) { 20 20 if (a.charAt(i)>=48 && a.charAt(i)<=57) { 21 21 b+= a.charAt(i); 22 22 } 23 23 } 24 24 } 25 25 System.out.println(b);//1654164115614161461 26 26 }