您可以使用正则表达式来提取字符串中的特定值。下面是一个示例代码,可以从字符串中提取出数字开头的部分:

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

public class Main {
    public static void main(String[] args) {
        String str1 = "0.7.3-grg-0.12.3";
        String str2 = "10.17.23223-grg-0.12.3";

        String pattern = "(\\d+\\.\\d+\\.\\d+)";
        Pattern regexPattern = Pattern.compile(pattern);
        
        Matcher matcher1 = regexPattern.matcher(str1);
        if (matcher1.find()) {
            System.out.println(matcher1.group(1));
        }
        
        Matcher matcher2 = regexPattern.matcher(str2);
        if (matcher2.find()) {
            System.out.println(matcher2.group(1));
        }
    }
}

运行这段代码,将输出:

0.7.3
10.17.23223

这样,您就可以从给定的字符串中提取出所需的值。

编写java 代码从这个 字符串中073-grg-0123 获取 073 这个值 或从这个 字符串中101723223-grg-0123 获取101723223 这个值

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

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