A text tokenizer in C ++ that allows multiple delimiters to be used

Possible duplicate:
C ++: how to split a string?

Is there a way tokenize strings in C ++ with multiple delimiters? In C #, I would do:

string[] tokens = "adsl, dkks; dk".Split(new [] { ",", " ", ";" }, StringSplitOptions.RemoveEmpty); 
+7
c ++ string c # tokenize
source share
2 answers

Use boost :: tokenizer. It supports multiple delimiters.

In fact, you don’t even need boost :: tokenizer. If all you need is a split, use boost :: split. The documentation shows an example: http://www.boost.org/doc/libs/1_42_0/doc/html/string_algo/usage.html#id1718906

+3
source share

Something like this will do:

 void tokenize_string(const std::string &original_string, const std::string &delimiters, std::vector<std::string> *tokens) { if (NULL == tokens) return; size_t pos_start = original_string.find_first_not_of(delimiters); size_t pos_end = original_string.find_first_of(delimiters, pos_start); while (std::string::npos != pos_start) { tokens->push_back(original_string.substr(pos_start, pos_end - pos_start)); pos_start = original_string.find_first_not_of(delimiters, pos_end); pos_end = original_string.find_first_of(delimiters, pos_start); } } 
+1
source share

All Articles