您的位置:首页 > 其它

MFC里面的CString类竟然没有Split函数,不能忍受,自己写一个

2011-04-30 18:42 1001 查看
用过Java的同学应该知道,String.Split()函数是多么的好用,但今天写MFC程序的时候突然要用到该功能函数,但苦苦搜寻msdn没有找到类似的函数。没办法,自己写个

CStringArray* Split(CString str, char ch)
{
// 去掉两边的ch
str.TrimLeft(ch);
str.TrimRight(ch);

int nStart = 0;
int nLastStart = 0;

CStringArray *pStrArray = new CStringArray();

while(-1 != (nStart = str.Find(ch, nStart))){
if(nLastStart != nStart){// 不是连续ch,
pStrArray->Add(str.Mid(nLastStart, nStart - nLastStart));
}
nStart ++;
nLastStart = nStart;
}
pStrArray->Add(str.Mid(nLastStart));	// 最后一个子串
return pStrArray;
}


如str="@I@@LOVE@C++@!", ch='@';

返回字符串数组{"I", "LOVE", "C++", "!"}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐