I have a vector
containing strings
that follow the format of text_number-number
Eg: Example_45-3
Using @Pixelchemist's answer and e.g. std::stoul
:
bool getFirstNumber(std::string const & a_str, unsigned long & a_outVal)
{
auto pos = a_str.find_first_of("0123456789");
try
{
if (std::string::npos != pos)
{
a_outVal = std::stoul(a_str.substr(pos));
return true;
}
}
catch (...)
{
// handle conversion failure
// ...
}
return false;
}