cstring字符串分割成按空格子字符串

环境vs2010
新建一个windows console apllication
将工程设为unicode编码 右击工程-》属性-》配置属性-》调试下面的字符集设置为用unicode编码。

此程序是将cstring类型的字符串,根据里面的空格,切割出若干个子字符串。

 

 

#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
string s[100];
int main()
{

 CString name(_T("我 是 janifer"));
 const char* split = " ";
 char* p;
 int n=name.GetLength();
 int len=WideCharToMultiByte(CP_ACP,0,name,name.GetLength(),NULL,0,NULL,NULL);
 char *pstr =new char[len+1] ;
 WideCharToMultiByte(CP_ACP,0,name,name.GetLength(),pstr,len,NULL,NULL);
 pstr[len] = '\0';
 p = strtok (pstr,split);
 int num =0;
 while(p!=NULL) {
  s[num]=p;
  
  num++;
  p = strtok(NULL,split);
 }
 for(int ii=0;ii<3;ii++)
 {
  cout<  }
 
 getchar();
 return 0;
}

你可能感兴趣的:(c++编程)