过滤掉字符串中的非数字

来源:互联网 发布:胡克霍根身体数据 编辑:程序博客网 时间:2024/06/05 13:52

代码中遇到需要将日期格式“2017-11-10 20:00:00”替换为“20171110200000”的需求,可通过time.replaceAll("[-\\s:]","");实现。


更通用的可通过pattern + Matcher的方式实现。

public static void filter(String s){  
        String regEx = "[^0-9]";  
        Pattern p = Pattern.compile(regEx);  
        Matcher m = p.matcher(s);  
        System.out.println(m.replaceAll("").trim());  
    }  

原创粉丝点击