【问题标题】:C++/Boost split a string on more than one characterC++/Boost 在多个字符上拆分字符串
【发布时间】:2013-04-03 13:58:35
【问题描述】:

一旦我看到一个例子,这可能真的很简单,但是我如何概括 boost::tokenizer 或 boost::split 来处理由多个字符组成的分隔符?

例如,使用“__”,这些标准拆分解决方案似乎都不起作用:

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);
}

boost::split(result, myString, "___");

【问题讨论】:

标签: c++ string parsing boost tokenize


【解决方案1】:
boost::algorithm::split_regex( result, myString, regex( "___" ) ) ;

【讨论】:

  • 你能告诉我要包含哪些头文件吗?
  • @Kadiam 尝试:#include
【解决方案2】:
【解决方案3】:

非升压解决方案

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    相关资源
    最近更新 更多