Java 测试:从字符串里面提取数字

|--需求说明

|--实现思路

1、将字符串拆解成字符数组

2、遍历这个数组,使用正则判断每个字符是不是数字

|--代码内容

 1 /**
 2  * @auther::9527
 3  * @Description: 提取字符中的数字
 4  * @program: 多线程
 5  * @create: 2019-08-09 18:08
 6  */
 7 public class Second {
 8     public static void main(String[] args) {
 9         //获得字符串
10         String before = "iu7i8hy4jnb2";
11         //拆分字符串
12         String[] temp = before.split("");
13         StringBuffer after = new StringBuffer("");
14         //遍历数组
15         for (int i = 0; i < temp.length; i++) {
16             // 使用正则,判断每一个字符是否是数字
17             if (temp[i].matches("[0-9]")){
18                 after.append(temp[i]);
19             }
20         }
21         System.out.println(after.toString());
22     }
23 }
使用正则的方法从字符串里面提取数字

|--运行结果

猜你喜欢

转载自www.cnblogs.com/twuxian/p/11328978.html