我有一个String的以下两个可能的内容.
显然金额总是变化的,我想提取关键信息和
Case 0: pricesString = ""
Case 1: pricesString = "$0.023"
Case 2: pricesString = "10+: $1.46 100+: $0.16 500+: $0.04"
在案例0中,我想什么都不做.
在案例1中,我想执行:
article.addPrice(1, 0.023);
在案例2中,我想执行:
article.addPrice(10, 1.46);
article.addPrice(100, 0.16);
article.addPrice(500, 0.04);
如何提取此信息,以便可以使用包含的浮点数和整数值调用article.addPrice?
解决方法:
这看起来像正则表达式的工作:
String pricesString = "10+: $1.46 100+: $0.16 500+: $0.04";
Pattern p = Pattern.compile("(\\d+)\\+: \\$(\\d\\.\\d\\d)");
Matcher m = p.matcher(pricesString);
while (m.find()) {
Intger.parseInt(m.group(1));
Double.parseDouble(m.group(2));
}
您可以通过简单的.length()检查在3种情况之间进行选择.上面的代码适用于最后一种情况.剩下的就是eaiser
标签:java,string,regex,floating-point,integer
来源: https://codeday.me/bug/20191007/1865570.html