1.建立Pattern 类对象
Pattern pattern=Pattern.compile(regex);
2.得到Mattcher对象
Mattcher matcher=pattern.matcher(input);
3.使用boolean find()方法和String group()方法结合使用得到分解的字符串
//Pattern 和Matcher类
String input="苹果:18.4元,橘子:24.3元,西瓜:24.7元";
String regex="[0123456789.]+";
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(input);
double count=0.0;
while(matcher.find()){
String item= matcher.group();
System.out.println(item);
count=count+Double.parseDouble(item);
}
System.out.println("总价格:"+count);
313

被折叠的 条评论
为什么被折叠?



