#include <iostream>
#include <string>
#include <vector>
/*
* @brife 字符串分割
* @param strtem 字符串数据
* @param a 用于分割的字符串
* @param res 分割后的数据结果
* */
static void split(const std::string& strtem, const std::string a, std::vector<std::string>& res)
{
res.clear();
std::string::size_type pos1, pos2;
pos2 = strtem.find(a);
pos1 = 0;
while (std::string::npos != pos2) {
res.push_back(strtem.substr(pos1, pos2 - pos1));
pos1 = pos2 + a.size();
pos2 = strtem.find(a, pos1);
}
res.push_back(strtem.substr(pos1));
}
int main(void)
{
std::vector<std::string> res;
split(R"(123,456,789)", ",", res);
std::cout << "英文字符分割:" << std::endl;
for (uint32_t level1 = 0; level1 < res.size(); ++level1)
{
std::cout << res[level1] << std::endl;
}
split(R"(123,456,789)", ",", res);
std::cout << "\n中文字符分割:" << std::endl;
for (uint32_t level1 = 0; level1 < res.size(); ++level1)
{
std::cout << res[level1] << std::endl;
}
return 0;
}
测试结果:
![](https://img-blog.csdnimg.cn/img_convert/1f2dfcb098449c6e09b75ba98c52b5bf.png)