'string' Parsing in C++

Hi all,

I want to parse a std::string, the delimitor is ','. For example,

string s = "13123123,3453453,6546456,25345345,52534534";

i want this to be in a list or vector.


Can anyone help me how to do this?
I used string class coz I thought there might be some easier facility than strtok
std::string::find(), std::string::substr().
ok guys, google helped me :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vector<string> tokenize(const string & str, const string & delim)
{
  vector<string> tokens;
  size_t p0 = 0, p1 = string::npos;

  while(p0 != string::npos)
  {
    p1 = str.find_first_of(delim, p0);
    if(p1 != p0)
    {
      string token = str.substr(p0, p1 - p0);
      tokens.push_back(token);
    }
    p0 = str.find_first_not_of(delim, p1);
 }
return tokens;

}
Last edited on
Can somebody please tell me how to convert long int to string?
Article on conversions:
http://cplusplus.com/articles/numb_to_text/

-Albatross
Using a stringstream and getline is another alternative. There's also Boost.Tokenizer.
Topic archived. No new replies allowed.