You'll need to think about what sort of algorithm you'd want to use to do this, as it's not entirely obvious. If a substring is asdf.1asdf
, should that be parsed as the decimal value 0.1
or simply 1
?
Also, can some of the embedded numbers be negative? If not this greatly simplifies the search space.
I think that aix is on the right track with using a regex, since once you come up with an algorithm this sounds like the kind of job for a state machine (scan through the input until you find a digit or optionally a -
or .
, then look for the next "illegal" character and parse the substring normally).
It's the edge cases that you have to think about though - for example, without negative numbers you can almost use s.split("[^0-9.]")
and filter out the non-empty elements. However, period characters that aren't part of a number will get you. Whatever solution you go with, think about whether any situations could trip it up.