A string tokenizer in C++ that allows multiple separators

Possible Duplicate:
C++: How to split a string?

Is there a way to tokenize a string in C++ with multiple separators? In C# I would have done:

string[] tokens = "adsl, dkks; dk".Split(new [] { ",", " ", ";" }, StringSplitOptions.RemoveEmpty);

Use boost::tokenizer. It supports multiple separators.

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


像这样的事情会做:

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);
        }
}
链接地址: http://www.djcxy.com/p/19586.html

上一篇: 在Python中分配空值或字符串

下一篇: C ++中的字符串标记器,允许使用多个分隔符