最近遇到了字符串的分割,自己参考了其他的做法,总结了下,
对于sep为单个字符是稳定的,但是对于多个就会出错,因为内部使用的是find_first_of
ErrCode SplitCsv(const string& src, const string& sep, vector<string>& dest)
{
if (src.empty())
return Err_StrEmpty;
dest.clear();
string subStr;
string::size_type start(0), index(0);
while (1)
{
index = src.find_first_of(sep, start);
subStr = src.substr(start, index - start);
dest.push_back(subStr);
start = src.find_first_not_of(sep, index);
if (start == string::npos)
break;
}
return No_Err;
}