A string tokenizer in C++ that allows multiple separators
Asked Answered
G

3

8

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);
Guaco answered 16/4, 2010 at 15:19 Comment(6)
@Nick Presta: Yes, but most of the questions I saw only involves using one delimiter.Guaco
How is that a dup? One wanted to split solely on white space, this wants to split on multiple delimiters.Septi
@Duck: Almost all of those answers can be adapted to handle any delimiters (most already do based on a parameter). Unless you're suggesting we have a new question for every type of delimiter?Lavettelavigne
Don't reinvent the wheel at all...seem my link boost::splitSayed
@Nick Presta - Fair enough. I didn't read through all the answers and I tripped across ones using streams to strip whitespaceSepti
How about some of the examples from the following: codeproject.com/KB/recipes/Tokenizer.aspx They are very efficient and somewhat elegant. The String Toolkit Library makes complex string processing in C++ simple and easy.Woodhead
S
3

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

Sayed answered 16/4, 2010 at 15:28 Comment(0)
B
2

Something like that 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);
        }
}
Biennial answered 16/4, 2010 at 15:30 Comment(0)
P
0

Here is my version (not heavily tested (yet)):

std::vector<std::string> split(std::string const& s,
    std::vector<std::string> const& delims)
{
    std::vector<std::string> parts;

    std::vector<std::pair<std::string::size_type, std::string::size_type>> poss;
    poss.reserve(delims.size());

    std::string::size_type beg = 0;

    for(;;)
    {
        poss.clear();

        std::string::size_type idx = 0;
        for(auto const& delim: delims)
        {
            if(auto end = s.find(delim, beg) + 1)
                poss.emplace_back(end - 1, idx);
            ++idx;
        }

        if(poss.empty())
            break;

        std::sort(std::begin(poss), std::end(poss));

        auto old_beg = beg;

        for(auto pos: poss)
        {
            parts.emplace_back(std::begin(s) + beg,
                std::begin(s) + old_beg + pos.first);
            beg = pos.first + delims[pos.second].size();
        }
    }

    if(beg < s.size())
        parts.emplace_back(std::begin(s) + beg, std::end(s));

    return parts;
}
Paleontology answered 12/6, 2018 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.