This function do what you want to do. It basically iterate on the vector of delimiters, applying your function for each one :
vector<string> SplitStringMultipleStrParameters(string s,const vector<string>& separators) {
//The result to be returned
vector<string> result = { s };
//Iterate on the list of separators, so for each separators
for (const auto& sep : separators) {
//toReplaceBy will be the next vector of strings where it will iterate
vector<string> toReplaceBy, tempRes;
//For each strings, we will split on "sep", the separator
for (auto&a : result) {
//Because of the result vector being cleared every time your function get called
//It get in a temp vector that we will concatenate after
ParseStringByStringSeparator(a, sep, tempRes);
//Concatenation of theses vectors
toReplaceBy.insert(toReplaceBy.end(), tempRes.begin(), tempRes.end());
}
//Erasing all strings that are empty. C++11 code here required because of the lambda
toReplaceBy.erase(std::remove_if(toReplaceBy.begin(), toReplaceBy.end(),
[](const std::string& i) {
return i == "";
}), toReplaceBy.end());
//The vector containing strings to be splited is replaced by the split result on this iteration
result = toReplaceBy;
//And we will split those results using the next separator, if there's more separator to iterate on
}
return result;
}
Code to test :
string test = "Hello I am a string";
auto r = SplitStringMultipleStrParameters(test, { " ", "am" });
for (auto& a : r) {
std::cout << a << '\n';
}
It needs to be compiled with a C++11 compiler, and need to include the <algorithm>
header.
If your compiler does not compile C++11 code, here is the function :
vector<string> SplitStringMultipleStrParameters(string s,const vector<string>& separators) {
vector<string> result = { s };
for (const auto& sep : separators) {
vector<string> toReplaceBy, tempRes;
for (auto&a : result) {
ParseStringByStringSeparator(a, sep, tempRes);
toReplaceBy.insert(toReplaceBy.end(), tempRes.begin(), tempRes.end());
}
auto it = toReplaceBy.begin();
while (it != toReplaceBy.end()) {
if ((*it) == "")
it = toReplaceBy.erase(it);
else
++it;
}
result = toReplaceBy;
}
return result;
}