码迷,mamicode.com
首页 > 其他好文 > 详细

VC/MFC分割字符串(SplitString)返回CStringArray

时间:2014-07-22 23:17:33      阅读:1134      评论:0      收藏:0      [点我收藏+]

标签:http   使用   os   strong   for   re   

引自:http://bbs.csdn.net/topics/60321228

原版:

CStringArray* SplitString(CString string, char pattern)

{

CStringArray* strArray = new CStringArray();

CString strTemp;

char c;

for(int i=0;i<string.GetLength();i++)

{

c=string[i];

if(c==pattern)

{

strArray->Add(strTemp);

strTemp="";

}

else

{

strTemp+=c;

}

}

strArray->Add(strTemp);

 

return strArray;

}

修改版(以单个字符分割)

CStringArray* SplitString(CString string, char pattern)

{

CStringArray* strArray = new CStringArray();

CString strTemp;

strTemp = string;

int iPos = 0;

while(iPos != -1)

{

iPos = strTemp.Find(pattern);

if(iPos == -1)

{

break;

}

strArray->Add(strTemp.Left(iPos));

strTemp = strTemp.Mid(iPos+1, strTemp.GetLength());

}

strArray->Add(strTemp);

 

return strArray;

}

修改版2(以多个字符分割)

CStringArray* SplitString(CString string, char pattern[])

{

int nPattern = strlen(pattern);

 

CStringArray* strArray = new CStringArray();

CString strTemp;

char c;

BOOL bFind = FALSE;

for(int i=0; i<string.GetLength(); i++)

{

c = string[i];

for(int j=0; j<nPattern; j++)

{

if(c == pattern[j])

{

if(strTemp != "")

{

strArray->Add(strTemp);

}

bFind = TRUE;

break;

}

bFind = FALSE;

}

if(bFind)

{

strTemp = "";

}

else

{

strTemp += c;

}

}

if(strTemp != "")

{

strArray->Add(strTemp);

}

 

return strArray;

}

使用:

char s[] = {‘ ‘, ‘,‘, ‘\0‘};

CStringArray strPolygon = *SplitString(strTemp, s);

VC/MFC分割字符串(SplitString)返回CStringArray,码迷,mamicode.com

VC/MFC分割字符串(SplitString)返回CStringArray

标签:http   使用   os   strong   for   re   

原文地址:http://www.cnblogs.com/doudongchun/p/3699640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!