C++/Boost split a string on more than one character

泪湿孤枕 提交于 2019-12-01 20:59:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!