Alex Rivera | Logout

How to parse complex string with C++?

Asked 2010-01-15T16:22:10.793
12

I'm trying to figure out how could I parse this string using "sstream" and C++

The format of it is: "string,int,int".

I need to be able to assign the first part of the string which contains an IP address to a std::string.

Here is an example of this string:

std::string("127.0.0.1,12,324");

I would then need to obtain

string someString = "127.0.0.1";
int aNumber = 12;
int bNumber = 324;

I will mention again that I can't use boost library, just sstream :-)

Thanks

Edit
Report

1 Answer

6

It isn't fancy but you can use std::getline to split the string:

std::string example("127.0.0.1,12,324");
std::string temp;
std::vector<std::string> tokens;
std::istringstream buffer(example);

while (std::getline(buffer, temp, ','))
{
    tokens.push_back(temp);
}

Then you can extract the necessary information from each of the separated strings.

answered 2010-01-15T16:33:28.333

Your Answer