Romz
Romz

Reputation: 1457

How to split a string and keep the delimiters using boost::split?

I have a string like this:

std::string input("I #am going to# learn how #to use #boost# library#");

I do this:

std::vector<std::string> splitVector;
boost::split(splitVector, input, boost::is_any_of("#"));

And got this: (splitVector)

splitVector:
        "I "
        "am going to"
        " learn how " 
        "to use "
        "boos"
        " library"
        "" // **That's odd, why do I have an empty string here ?**

But need something like this:

splitVector:
    "I "
    "#am going to"
    "# learn how "
    "#to use "
    "#boost"
    "# library"
    "#"

How to do that ? Or maybe there is another way to do it in boost library ? And why do I get an empty string in splitVector ?

Upvotes: 4

Views: 2788

Answers (1)

mockinterface
mockinterface

Reputation: 14890

You cannot use boost::split because the internal implementation that uses the split_iterator from boost/algorithm/string/find_iterator.hpp swallows the tokens.

However you can get by with boost::tokenizer, as it has an option to keep the delimiters:

Whenever a delimiter is seen in the input sequence, the current token is finished, and a new token begins. The delimiters in dropped_delims do not show up as tokens in the output whereas the delimiters in kept_delims do show up as tokens.
http://www.boost.org/doc/libs/1_55_0/libs/tokenizer/char_separator.htm

See next live:

#include <iostream>
#include <string>
#include <boost/tokenizer.hpp>

int main() {
    // added consecutive tokens for illustration
    std::string text = "I #am going to# learn how ####to use #boost# library#";    
    boost::char_separator<char> sep("", "#"); // specify only the kept separators
    boost::tokenizer<boost::char_separator<char>> tokens(text, sep);
    for (std::string t : tokens) { std::cout << "[" << t << "]" << std::endl; }
}
/* Output:
[I ]
[#]
[am going to]
[#]
[ learn how ]
[#]
[#]
[#]
[#]
[to use ]
[#]
[boost]
[#]
[ library]
[#] */

Upvotes: 6

Related Questions