我正在使用 boost 字符串库,刚刚发现 split 方法非常简单。
string delimiters = ",";
string str = "string, with, comma, delimited, tokens, \"and delimiters, inside a quote\"";
// If we didn't care about delimiter characters within a quoted section we could us
vector<string> tokens;
boost::split(tokens, str, boost::is_any_of(delimiters));
// gives the wrong result: tokens = {"string", " with", " comma", " delimited", " tokens", "\"and delimiters", " inside a quote\""}
这会很好而且简洁......但是它似乎不适用于引号,而是我必须做类似以下的事情
string delimiters = ",";
string str = "string, with, comma, delimited, tokens, \"and delimiters, inside a quote\"";
vector<string> tokens;
escaped_list_separator<char> separator("\\",delimiters, "\"");
typedef tokenizer<escaped_list_separator<char> > Tokeniser;
Tokeniser t(str, separator);
for (Tokeniser::iterator it = t.begin(); it != t.end(); ++it)
tokens.push_back(*it);
// gives the correct result: tokens = {"string", " with", " comma", " delimited", " tokens", "\"and delimiters, inside a quote\""}
我的问题是当您引用分隔符时可以使用 split 或其他标准算法吗?感谢 Purpledog,但我已经有了一种未弃用的方法来实现所需的结果,我只是认为它非常麻烦,除非我可以用更简单、更优雅的解决方案替换它,否则我一般不会在不先将其包装起来的情况下使用它还有另一种方法。
编辑:更新了代码以显示结果并澄清问题。