6

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, "___");
daj
  • 6,962
  • 9
  • 45
  • 79

3 Answers3

10
boost::algorithm::split_regex( result, myString, regex( "___" ) ) ;
1

you have to use splitregex instead: http://www.daniweb.com/software-development/cpp/threads/118708/boostalgorithmsplit-with-string-delimeters

Penfold
  • 599
  • 4
  • 8
1

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;
}
gehad
  • 1,205
  • 3
  • 12
  • 17