String tokenizer for CPP String?

I want to use a string tokenizer for a CPP string, but all I could find was for Char *. Is there something similar for a CPP string?

+7
c ++ string tokenize
source share
5 answers

What do you mean by token? If this is something separated by spaces, line streams are what you want:

std::istringstream iss("blah wrxgl bxrcy") for(;;) { std::string token; if(!(iss>>token)) break; process(token); } if(!iss.eof()) report_error(); 

Alternatively, if you are looking for a separate separator character, you can replace iss>>token with std::getline(iss,token,sep_char) .

If it is more than one character that can act as a delimiter (and if it is not spaces), use combinations of std::string::find_first() and std::string::substr() .

+7
source share

You can do as chubsdad said, or use the boost tokenizer: http://www.boost.org/doc/libs/1_44_0/libs/tokenizer/tokenizer.htm

Doing it yourself is not that difficult if you are afraid of Boost.

+4
source share

You should take a look at Boost Tokenizer

+1
source share

Check out STL algos like find_first_of, find_first_not_of etc. to create a custom one.

0
source share

Try this snippet I found somewhere (maybe even here?):

 #include <string> #include <vector> #include <sstream> std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) { std::stringstream ss(s); std::string item; while(std::getline(ss, item, delim)) { elems.push_back(item); } return elems; } std::vector<std::string> split(const std::string &s, char delim) { std::vector<std::string> elems; return split(s, delim, elems); } 
0
source share

All Articles