有一个CString,内容很长,怎样按空格分割?

xuleilei111 2009-01-01 02:16:23
比如
CString a="0 1 2 3 4 5 6 7 8 9 10 11 ......N",b[M];//这个很长
怎么编程才能分割成这样(要求如下)?
b[0]=0;
b[1]=1;
b[2]=2;
.
.
.
.
.
.



注:b[M]也是CString类型
...全文
1651 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangpian 2011-01-08
  • 打赏
  • 举报
回复
三楼,你好,我要是把代码改成:
CString strTmp;
int i = 0;
int j=0;
while(j<10)
{
while ( AfxExtractSubString(strTmp, a, i, _T(' ')) )
{b[i++] = strTmp;}
j++;
}好像第二次循环,就是j=1以后,AfxExtractSubString那个循环就不执行了,怎么回事呀?
winnerforever_1 2010-02-23
  • 打赏
  • 举报
回复
这个就可以了!!!!!!!!!!!!!
wutaihua 2009-01-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 TearyWang 的回复:]
C/C++ code
CString strTmp;
int i = 0;
while ( AfxExtractSubString(strTmp, a, i, _T(' ')) )
b[i++] = strTmp;
[/Quote]

这个就可以了
野男孩 2009-01-01
  • 打赏
  • 举报
回复
复制一份出来,用strtok函数很方便。
黑色coder 2009-01-01
  • 打赏
  • 举报
回复
tokenize()试一试
lizhigang34 2009-01-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 nostopping 的回复:]
请问楼上的:能把在SplitString函数中定义的指针作为返回值吗?
[/Quote]
应该可以吧,SplitString()这个函数写的不是很好,但应该能解决问题..
nostopping 2009-01-01
  • 打赏
  • 举报
回复
请问楼上的:能把在SplitString函数中定义的指针作为返回值吗?
lizhigang34 2009-01-01
  • 打赏
  • 举报
回复
以前就有人问过相同的问题,VC怎么像C#的string::Split()函数那样分割字符串.

// Test3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "SplitStr.h"


#include <tchar.h>
#define _UNICODE
#define UNICODE



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

//功能: 分割字符串,类似C#字符串分割函数Split()
//参数: str:输入字符串,待分割. split:分割字符. iSubStrs返回子字符串的数量
//返回: 指向分割后的字符串指针
CString * SplitString(CString str, char split, int& iSubStrs);


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
//cout << (LPCTSTR)strHello << endl;
}

CString str = "0 1 2 3 4 5 6 7 8 9 10 11";
CString* pStr;
int iSubStrs;
pStr = SplitString(str, ' ', iSubStrs);
//如果子字符串的数量为1
if (iSubStrs == 1)
{
//Convert CString to char
char* pCh = (LPSTR)(LPCTSTR)str;
printf("%s\n", pCh);
}
else
{
//输出所有子字符串
for (int i = 0; i < iSubStrs; i++)
{
//Convert CString to char
char* pCh = (LPSTR)(LPCTSTR)pStr[i];
printf("%s\n", pCh);
}
delete []pStr;
}

system("pause");

return nRetCode;
}


CString * SplitString(CString str, char split, int& iSubStrs)
{
int iPos = 0; //分割符位置
int iNums = 0; //分割符的总数
CString strTemp = str;
CString strRight;
//先计算子字符串的数量
while (iPos != -1)
{
iPos = strTemp.Find(split);
if (iPos == -1)
{
break;
}
strRight = strTemp.Mid(iPos + 1, str.GetLength());
strTemp = strRight;
iNums++;
}
if (iNums == 0) //没有找到分割符
{
//子字符串数就是字符串本身
iSubStrs = 1;
return NULL;
}
//子字符串数组
iSubStrs = iNums + 1; //子串的数量 = 分割符数量 + 1
CString* pStrSplit;
pStrSplit = new CString[iSubStrs];
strTemp = str;
CString strLeft;
for (int i = 0; i < iNums; i++)
{
iPos = strTemp.Find(split);
//左子串
strLeft = strTemp.Left(iPos);
//右子串
strRight = strTemp.Mid(iPos + 1, strTemp.GetLength());
strTemp = strRight;
pStrSplit[i] = strLeft;
}
pStrSplit[iNums] = strTemp;
return pStrSplit;
}

yang_fujiang 2009-01-01
  • 打赏
  • 举报
回复
关注 学习中
TearyWang 2009-01-01
  • 打赏
  • 举报
回复

CString strTmp;
int i = 0;
while ( AfxExtractSubString(strTmp, a, i, _T(' ')) )
b[i++] = strTmp;
oyljerry 2009-01-01
  • 打赏
  • 举报
回复
或者自己实现,Find查找空格,自己接着截取
harry330 2009-01-01
  • 打赏
  • 举报
回复
用strtok函数即可。

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧