Java 截取字符串中的数字: 使用正则表达式提取波浪线前后数字
你可以使用正则表达式来截取波浪线前面和后面的数字。下面是一个示例代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String input = '0~320';
String pattern = '(\d+)~(\d+)';
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
if (m.find()) {
String before = m.group(1);
String after = m.group(2);
System.out.println('前面的数字: ' + before);
System.out.println('后面的数字: ' + after);
} else {
System.out.println('没有匹配到数字');
}
}
}
输出结果为:
前面的数字: 0
后面的数字: 320
原文地址: https://www.cveoy.top/t/topic/qhRF 著作权归作者所有。请勿转载和采集!