I want to use string Tokenizer for CPP string but all I could find was for Char*. Is there anything similar for CPP string ?
Thanks in advance
I want to use string Tokenizer for CPP string but all I could find was for Char*. Is there anything similar for CPP string ?
Thanks in advance
Check out STL algos like *find_first_of*, *find_first_not_of* and so on to create a custom one.
You can do as said by chubsdad or use boost tokenizer : http://www.boost.org/doc/libs/1_44_0/libs/tokenizer/tokenizer.htm
Doing it by yourself is not so complicated if you're affraid by Boost.
What do you mean by "token"? If it's something separated by any whitespace, the string streams is 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 your looking for a a certain single separating character, you can replace iss>>token
with std::getline(iss,token,sep_char)
.
If it's more than one character that can act as a separator (and if it's not whitespaces), a combinations of std::string::find_first()
and std::string::substr()
should do.
Try this snippet I found somewhere (maybe even around 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);
}