问题
This is probably really simple once I see an example, but how do I generalize boost::tokenizer or boost::split to deal with separators consisting of more than one character?
For example, with "__", neither of these standard splitting solutions seems to work :
boost::tokenizer<boost::escaped_list_separator<string> >
tk(myString, boost::escaped_list_separator<string>("", "____", "\""));
std::vector<string> result;
for (string tmpString : tk) {
result.push_back(tmpString);
}
or
boost::split(result, myString, "___");
回答1:
boost::algorithm::split_regex( result, myString, regex( "___" ) ) ;
回答2:
you have to use splitregex instead: http://www.daniweb.com/software-development/cpp/threads/118708/boostalgorithmsplit-with-string-delimeters
回答3:
Non-boost solution
vector<string> split(const string &s, const string &delim){
vector<string> result;
int start = 0;
int end = 0;
while(end!=string::npos){
end = s.find(delim, start);
result.push_back(s.substr(start, end-start));
start = end + delim.length();
}
return result;
}
来源:https://stackoverflow.com/questions/15789449/c-boost-split-a-string-on-more-than-one-character