最近完成项目需要处理传输来的字符串
字符串类型是[x,x,x...],[y,y,y...],[1,2,3...],[q,e,r...],[1,1,t,...].......
必须保证传过来的字符串左括号和右括号数量一致,中间有其他字符也可以
例如"[1,2,3],[4,5,6],354[1,2,3],得得得[4,5,6]"
最后输出效果都是[1,2,3] [4,5,6] [1,2,3] [4,5,6]。
跟split分割效果不一样。
//函数
//需要自己确定字符串中有 几对中括号
void my_split(string &str0,int n,vector<string>&vs)
{
int pos1 = 0;
int pos2 = 0;
for (int i = 0; i < n; i++)
{
pos1 = str0.find('[',pos1);
pos2 = str0.find(']',pos2);
vs.push_back(str0.substr(pos1, pos2 - pos1 + 1));
pos1++;
pos2++;
}
}
主函数
int main()
{
string str0 = "[1,2,3],[4,5,6],[7,8,9]";
//根据协议进行更改
vector<string>vss;
my_split(str0, 3, vss);
cout << vss[0] << endl;
cout << vss[1] << endl;
cout << vss[2] << endl;
system("pause");
return 0;
}