应用一:
例如,String str = "2006-04-15 02:31:04",要把这个串变成20060415023104,你会怎么做?
采用正则表达式,代码如下:
package cn.wuhan.unit9; public class Qukong { public static void main(String[] args) { String str = "2006-04-15 02:31:04"; String str2 = ""; String[] result = str.split("\\D"); for(int i=0;i<result.length;i++){ System.out.println(result[i]); str2 += result[i]; } System.out.println(str2); } }
效果如下:
应用二:
String s = "32fdsf1er2te3er8yt9hg",从s中找出3212389,你会怎么做?
采用正则表达式,代码如下:
package cn.wuhan.unit9; public class Insert { static String s = "32fdsf1er2te3er8yt9hg"; public static void main(String[] args) { String a = s.replaceAll("[^0-9]",""); System.out.println(a); } }
效果如下: