Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Boost: Split function is_any_of()

Tags:

c++

boost

I'm trying to use the split() function provided in boost/algorithm/string.hpp in the following function :

vector<std::string> splitString(string input, string pivot) { //Pivot: e.g., "##"
    vector<string> splitInput;  //Vector where the string is split and stored
    split(splitInput,input,is_any_of(pivot),token_compress_on);       //Split the string
    return splitInput;
}

The following call :

string hello = "Hieafds##addgaeg##adf#h";
vector<string> split = splitString(hello,"##"); //Split the string based on occurrences of "##"

splits the string into "Hieafds" "addgaeg" "adf" & "h". However I don't want the string to be split by a single #. I think that the problem is with is_any_of().

How should the function be modified so that the string is split only by occurrences of "##" ?

like image 601
Enigman Avatar asked Dec 14 '12 09:12

Enigman


1 Answers

You're right, you have to use is_any_of()

std::string input = "some##text";
std::vector<std::string> output;
split( output, input, is_any_of( "##" ) );

update

But, if you want to split on exactly two sharp, maybe you have to use a regular expression:

 split_regex( output, input, regex( "##" ) ); 

take a look at the documentation example.

like image 131
Luca Davanzo Avatar answered Sep 27 '22 18:09

Luca Davanzo