MFC 字符串截取成数组 wcstok,mfcwcstok
投稿于 被查看 29483 次 评论:279
MFC 字符串截取成数组 wcstok,mfcwcstok
CString stext=_T("我,爱,你"); wchar_t str[100]; wcscpy(str,CT2CW(stext)); //非 unicode编码下 wcscpy(wszWideString, CA2CW(sText)); wchar_t *SEPS=L","; wchar_t *token; token=wcstok( str,SEPS); while(token!=NULL) { TRACE(token); token=wcstok(NULL,SEPS); }
输出: 我 爱 你
实现 MFC下 CString 字符串的截取,相当于C#的 split
CString m_str1="123,789,654,339";
int count = m_str1.Replace(',', ' ');
if(count<=0)
{
printf("No data");
return;
}
int* num = new int[count];
int pos = m_str1.Find(' ');
int i = 0;
while(pos != -1)
{
CString field = m_str1.Left(pos);
num [i] = atoi(field.GetBuffer(0));
i++;
m_str1 = m_str1.Right(m_str1.GetLength() - pos - 1);
pos = m_str1.Find(' ');
}
// last node
if(m_str1.GetLength()>0)
{
num [i] = atoi(m_str1.GetBuffer(0));
}
// do something elase you want
//...
//...
delete num ;
设:
CString str;
char *s;
则:
s = (char*)malloc(str.GetLength()+1);
strcpy(s,str.GetBuffer(0));
现在s就能当数组用了,s[3]='\n';什么的;
最后别忘了free(s);就行了
用户评论