I play with the boost strings library and just stumbled upon the amazing simplicity of the split method.
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\""}
Which would be nice and concise ... however it doesn't work with quotes, and instead I have to do something like the following
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);
My question can be split or is another standard algorithm used when you specify the delimiters? Thanks to purpledog, but I already have an outdated way to achieve the desired result, I just think that it is rather cumbersome, and if I can not replace it with a simpler and more elegant solution, I would not use it at all without first investing it in another method .
EDIT: Updated code to show results and clarify the question.