从这里拿来改进了点,亲测可用。
void stringSplit(const std::string& sourceStr, const std::string& delimiter, std::vector<std::string>& result)
{
size_t last = 0;
size_t next = 0;
std::string token;
while ((next = sourceStr.find(delimiter, last)) != std::string::npos)
{
if (next > last)
{
token = sourceStr.substr(last, next-last);
result.push_back(token);
}
last = next+delimiter.length();
}
if (last < sourceStr.length())
{
token = sourceStr.substr(last);
result.push_back(token);
}
}
测试如下,
int main(void)
{
std::string s = "scott>=tiger>=mushroom";
std::string delimiter = ">=";
std::vector<std::string> result;
stringSplit(s, delimiter, result);
for (auto it = result.cbegin(); it != result.cend(); ++it)
{
std::cout << *it << "\n";
}
return 0;
}
结果如下,
|