欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

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


急VC++MFC中怎把一个字符串按分隔符分割成字符串数组,再把字符串数组转化成整型数组?

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 ;
 

MFC 怎把字符串转换为数组

设:
CString str;
char *s;
则:
s = (char*)malloc(str.GetLength()+1);
strcpy(s,str.GetBuffer(0));
现在s就能当数组用了,s[3]='\n';什么的;
最后别忘了free(s);就行了
 

相关频道:

用户评论