加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式去掉非数字字符

发布时间:2020-12-14 01:43:23 所属栏目:百科 来源:网络整理
导读:应用一: 例如,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 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);
	}

}

效果如下:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读