C语⾔字符串拆分成字符串数组⽅法
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
typedef vector <string> StringArrayC;
bool Str2StrArray(const string& strSource, char separator, StringArrayC& target)
{
target.clear();
size_t length = strSource.size();
if (length == 0)
{
return false;
}
if (NULL == separator)
{
return false;
}
stringstream ss(strSource);
string item = "";
while (getline(ss, item, separator))
{
if (item.size() != 0)
{
target.push_back(item);
}
}
if (0 == target.size())
{
return false;
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *strSource = "sfjds,12,wej,sewjei,ff,djfi";
char separator = ',';
StringArrayC target;
Str2StrArray(strSource, separator, target);
for (int i= 0; i < target.size(); i++)
{
cout << target.at(i) << endl;
}
getchar();
return 0;
数组转换成字符串}