I am surprised that no one mentioned regular expressions. They were added as part of TR1 and are included in Boost . Here's a solution using regex's
typedef std::tr1::match_results<std::string::const_iterator> Results; std::tr1::regex re(":[[:space:]]+([[:digit:]]+)", std::tr1::regex::extended); std::string str("Sectors: 4095"); Results res; if (std::tr1::regex_search(str, res, re)) { std::cout << "Number found: " << res[1] << std::endl; } else { std::cerr << "No number found." << std::endl; }
It seems like a lot more work, but you will get more from him IMHO.
D.Shawley
source share