C#可以直接使用函数库自带的split C++不行, 所以利用string的find函数和substr函数自己写了一个。
测试结果如下:
测试代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void split(vector<string>& result, string str, string pattern) {
string::size_type pos;
int sizep = pattern.size();
int sizes = str.size();
int head = 0, index = 0;
if (str.find(pattern, index) == 0)
index++, head = sizep;
while ((pos = str.find(pattern, index)) != -1) {
result.push_back(str.substr(head, pos - head));
index = pos + sizep;
head = pos + sizep;
}
if (head != 0 && head != sizes)
result.push_back(str.substr(head, sizes - head));
}
int main() {
vector<string> chr;
split(chr, ",dasfdsaf,dsaf,fdsa,df,", ",");
for (auto i : chr)
cout << i << " ";
}
|