Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to extract last figure after dot in a string like "7.8.9.1.5.1.100"

Tags:

c++

string

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.

like image 523
AJ. Avatar asked Feb 08 '11 12:02

AJ.


2 Answers

std::string has a rfind() method; that will give you the last . From there it's a simple substr() to get the string "100".

like image 101
MSalters Avatar answered Oct 16 '22 19:10

MSalters


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());
}
like image 3
Benoit Avatar answered Oct 16 '22 20:10

Benoit