目的:
把一个带逗号的字符串比如“”aa,bbb,cc,dd“”分成四个小的字符串,不用返回值
#include <iostream>
#include <string>
using namespace std;
void split(string input)
{
string temp[4]; //长字符串分成4段小字符串
int j= 0;
for (unsigned i = 0; i < input.length(); i++)
{
if (input[i] != ',')
temp[j] += input[i];//temp[i]字符串=input中的字母连字母
else
j++;
}
for (int j = 0; j <4; j++)
cout << temp[j] << endl;
}
int main()
{
string fullstring = "aa,bbb,cc,ddd";
split(fullstring);
system("pause");
return EXIT_SUCCESS;
}
debug收获:
之前用的头文件#include <cstring>,它主要处理char,char*类型的数据,所以总在报错
改之后,用#include <string>,它主要处理string类型的数据,所以编译通过
运行结果如图: