java实现输出字符串中的数字字符

来源:互联网 发布:ubuntu自带游戏有哪些 编辑:程序博客网 时间:2024/05/03 13:39

package com.joe;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test
{
    //将字符串的数字字符输出
    public static void main(String[] args)
    {
        String str = "iu7i8hy4jnb2";
       
        //方法一
        String vString = str.replaceAll("[//D]", "");    // [/D] == [^/d] == [^0-9]
        System.out.println(vString);
       
        //方法二
        String regex = "//d";
        Matcher m = Pattern.compile(regex).matcher(str);
        while(m.find())
        {
            System.out.print(m.group());
        }
        System.out.println();
    }
}

原创粉丝点击