I need to extract last number after last dot in a C++ string like "7.8.9.1.5.1.100" and store it in an integer??
Added: This string could also be "7.8.9.1.5.1.1" or "7.8.9.1.5.1.0".
I would also like to validate that that its is exactly "7.8.9.1.5.1" before last dot.
std::string
has a rfind()
method; that will give you the last .
From there it's a simple substr()
to get the string "100"
.
const std::string s("7.8.9.1.5.1.100");
const size_t i = s.find_last_of(".");
if(i != std::string::npos)
{
int a = boost::lexical_cast<int>(s.substr(i+1).c_str());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With