使用正则表达式
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String input = "你好6.6%";
double number = extractNumber(input);
System.out.println(number);
}
private static double extractNumber(String input) {
Pattern pattern = Pattern.compile("(\\d+\\.\\d+)"); // 匹配数字,包括整数和小数
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
String numberStr = matcher.group();
return Double.parseDouble(numberStr);
} else {
return 0; // 如果没有找到数字,返回0
}
}
}
标签:数字,matcher,number,汉字,Pattern,字符串,input,String
From: https://www.cnblogs.com/fchhk-blog/p/17965547