我有一个看起来像"A = 1.23; B = 2.345; C = 3.567"的字符串
我只对"C = 3.567"感兴趣
到目前为止我所拥有的是:
Matcher m = Pattern.compile("C=\\d+.\\d+").matcher("A=1.23;B=2.345;C=3.567");
while(m.find()){
double d = Double.parseDouble(m.group());
System.out.println(d);
}
Run Code Online (Sandbox Code Playgroud)
问题是它显示3与567分开
输出:
3.0
567.0
我想知道如何包含小数,所以它输出"3.567"
编辑:我还想匹配C,如果它没有小数点:所以我想捕获3567以及3.567
因为C =也被内置到模式中,如何在解析双精度之前将其删除?
我有一个尝试将字符串转换为long的方法:
public static Long getLong(String str) {
long multiplier = 1;
try {
Long value = -1L;
str = str.replaceAll("\\s", ""); // remove whitespace
str = str.replaceAll("%", "");
str = str.replaceAll(",", "");
if (str.contains("M")) {
str = str.replaceAll("M", "");
multiplier = 1000000;
} else if (str.contains("B")) {
str = str.replaceAll("B", "");
multiplier = 1000000000;
} else if (str.contains("K")){
str = str.replaceAll("K", "");
multiplier = 1000;
}
// an exception is thrown for values like 199.00, so removing
// decimals if …Run Code Online (Sandbox Code Playgroud)