16,472
社区成员
发帖
与我相关
我的任务
分享
int split(const string& str, vector<string>& ret_, const string &sep )
{
if (str.empty())
{
return 0;
}
string tmp;
string::size_type pos_begin = str.find_first_not_of(sep);
string::size_type comma_pos = 0;
while (pos_begin != string::npos)
{
comma_pos = str.find(sep, pos_begin);
if (comma_pos != string::npos)
{
tmp = str.substr(pos_begin, comma_pos - pos_begin);
pos_begin = comma_pos + sep.length();
}
else
{
tmp = str.substr(pos_begin);
pos_begin = comma_pos;
}
if (!tmp.empty())
{
ret_.push_back(tmp);
tmp.clear();
}
}
return 0;
}
int main()
{
string s = "750,,,,,,-750,,,742.562,,2020.033,,2020.033";
vector<string> res;
split(s, res, ",");
string result;
for(auto item : res)
{
if(!item.empty())
result += (item+",");
}
if(!result.empty())
result = result.substr(0, result.size()-1);
return 0;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
char inStr[]=",,750,,,,,,-750,,,742.562,,2020.033,,2022.033,,,";
char *pStrStart=inStr;
int all=strlen(inStr);
char *pStrEnd=pStrStart+all;
for(int i=0; i<all; i++)
{
if(inStr[i]==',')
{
inStr[i]=0;
}
}
//
int slen=0;
while(pStrStart < pStrEnd)
{
slen=strlen(pStrStart);
if(slen)
{
afxDump << pStrStart<< "\n";
// CStringArray strAr;
// strAr.Add(pStrStart);
}
pStrStart += slen;// move to string end
if(pStrStart < pStrEnd) pStrStart ++;// move to next str
}
return 0;
}