简体   繁体   中英

split using boost::is_any_of confuses over delimeter “,,” and “,”

I currently have a string that has the following structure

xxx,xxx,xxxxxxx,,xxxxxx,xxxx

Now I am using the following code

 std::vector< std::string > vct;
 boost::split( vct, str, boost::is_any_of(",,") );

Now the boost splits up the string once it finds "," and not ",," which I dont want. Is there any way that I could explicitly specify that it should split only if it finds ",," and not ","

is_any_of(",,") will match anything that's specified in the list. In this case either , or ,

What you are looking for is along the line of

boost::algorithm::split_regex( vct, str, regex( ",," ) ) ;

For future reference..

boost::split takes a 4th parameter eCompress that can lets you treat adjacent separators as a single separator:

eCompress

If eCompress argument is set to token_compress_on, adjacent separators are merged together. Otherwise, every two separators delimit a token.

All you have to do is specify the parameter. You can skip the second , too, as:

boost::split( vct, str, boost::is_any_of(","),
              boost::algorithm::token_compress_on)

Here's the documentation.

Is_any_of splits on any of the characters in the string. It will not do what you want. You need to look in the boost manual for another predicate.

Edit: Out of curiosity I went looking in the API myself, infortunantly I could not find a ready predicate for what you want. Worst case you hasve to write it yourself.

#include <functional>
#include <boost/algorithm/string/compare.hpp>
...

std::vector< std::string > vct;
//boost::split( vct, str, [](const auto& arg) { return arg == str::string(",,"); } );
boost::split( vct, str, std::bind2nd(boost::is_equal, std::string(",,")) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM