要提取字符串中的数字,可以使用正则表达式。下面是一个Java代码示例,演示如何提取字符串中的数字:

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

public class Main {
    public static void main(String[] args) {
        String str = "abc123def456";
        Pattern pattern = Pattern.compile("\\d+");  // 匹配一个或多个数字
        Matcher matcher = pattern.matcher(str);
        
        while (matcher.find()) {
            String number = matcher.group();
            System.out.println(number);
        }
    }
}

这段代码将输出字符串中的所有数字:123 和 456

Java代码字符串提取数字

原文地址: https://www.cveoy.top/t/topic/h25f 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录