Removing all non-doubles from a string

I have many random strings that look something like this:

" 55.343 Char 1.3825 asdf 0.1853 500 1.1359 4.0000 1 100 4.5043"

Notices how there are ints and chars and doubles in the string.

How do I remove all non-doubles for a string like this? The chars and ints may be anywhere within the string.
Last edited on
For your (apparent) purposes, can I assume a double must have a fractional part? Meaning:

100 - int
100.0 - double

If so, then you need to find all instances of '.' surrounded by only digits.


There are several ways. The simplest might be to use a std::stringstream to break the string into whitespace delineated tokens.

For each token, check that it contains only one '.' and that the remaining characters are digits. If so, it is a double (and you can convert it to one). Otherwise it is safe to ignore.

Hope this helps.
http://www.cplusplus.com/forum/general/132573/

Get the doubles from the string and put them in a new string.
Yes, 100 would be an int and 100.0 would be a double.

Thanks for the tips.
Yay295,

That only works if the chars or ints are at the beginning of the string.

I am trying to extract only the doubles from a string when there are chars and ints randomly located in the string.
Actually I think I could probably edit that code to suit my needs... I don't know much about formatted input but I will learn. Thanks
To the stream input manipulators, "100" and "100.0" both look like doubles. Hence my suggestions.
Topic archived. No new replies allowed.