我有以下两个可能的字符串内容。
显然,金额总是有所不同,我想提取关键信息并
Case 0: pricesString = ""
Case 1: pricesString = "$0.023"
Case 2: pricesString = "10+: $1.46 100+: $0.16 500+: $0.04"
In Case 0
我什么都不想做。
In Case 1
我想表演:
article.addPrice(1, 0.023);
In Case 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));
}
您可以通过简单的操作在 3 种情况中进行选择.length()
查看。上面的代码适用于最后一种情况。剩下的就比较容易了
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)