vector<string> split(const string &str, const string &sep, bool allow_empty)
{
vector<string> result;
- unsigned start=str.find_first_not_of(sep);
+
+ unsigned start=0;
+ if(!allow_empty)
+ start=str.find_first_not_of(sep);
+
while(start<str.size())
{
unsigned end=str.find_first_of(sep, start);
result.push_back(str.substr(start, end-start));
- if(end==string::npos) break;
+
+ if(end==string::npos)
+ break;
+
if(allow_empty)
+ {
start=end+1;
+ if(start==str.size())
+ result.push_back(string());
+ }
else
start=str.find_first_not_of(sep, end);
}
+
return result;
}