Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

java - Getting a double out of a string

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...