从字符串中获取双倍 [英] Getting a double out of a string

查看:105
本文介绍了从字符串中获取双倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的字符串:
按照自己的方式行事,11.95苏格兰历史,14.50,一天学习微积分,29.95
有什么方法可以获得双打这个字符串?

i have a string containing the following: "Did It Your Way, 11.95 The History of Scotland, 14.50, Learn Calculus in One Day, 29.95" is there any way to get the doubles from this string?

推荐答案

使用正则表达式提取双精度数,然后使用Double.parseDouble()解析:

Use regular expressions to extract doubles, then Double.parseDouble() to parse:

Pattern p = Pattern.compile("(\\d+(?:\\.\\d+))");
Matcher m = p.matcher(str);
while(m.find()) {
    double d = Double.parseDouble(m.group(1));
    System.out.println(d);
}

这篇关于从字符串中获取双倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆