http://blog.csdn.net/yaerfeng/article/details/28855587
例子
package javastudy; public class Test3 { static String s = "sdgjerj5346m456n45653m4"; public static void main(String [] args){ String a = s.replaceAll("[^0-9]", ""); System.out.println(a); String str = "2006-04-06 02:31:04"; StringBuffer sb = new StringBuffer(); //\d正则表达式是非数字的意思以非数字分割字符串str,与[^0-9]等价 String [] result = str.split("\D"); for (int i=0;i<result.length;i++){ System.out.println(result[i]); sb.append(result[i]); } System.out.println(sb.toString()) ; String b = str.replaceAll("[^0-9]", ""); System.out.println(b); } }
结果为
5346456456534
2006
04
06
02
31
04
20060406023104
20060406023104