《》《》《》《》关于CString分割的问题《》《》《》《》

popyyb 2003-12-02 10:34:22
请问如何把CString= "a,b,c,d,...."

按,号分割存到一个数组中,数组的长度为分割的段数,谢谢
...全文
54 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sorcerercwy 2003-12-02
  • 打赏
  • 举报
回复
CString mString("aa,bbb,cccc,ddddd,......");
CString newString[100];
int newStringNum=0;

int mCharLength = mString.GetLength();
if ( mCharLength != 0)
{
int GroupNum=0;
int GroupPos[100];

//','的位置和个数
for(int i = 0; i < mCharLength ; i++)
{
if (mString.GetAt(i) == ',')
{
GroupPos[GroupNum++] = i;
}
}

CString temp = mString;
newString[0] = mString.Left(GroupPos[0]);
for (i = 1; i < GroupNum; i++)
{
temp = temp.Right(mCharLength-GroupPos[i-1]-1);
newString[i]=temp.Left(GroupPos[i]-GroupPos[i-1]-1);
}
newString[GroupNum]=mString.Right(mCharLength-GroupPos[GroupNum-1]-1);
newStringNum = GroupNum+1;
}

把上面的CString newString[100] 定义成链表,应该可以符合你的要求把
popyyb 2003-12-02
  • 打赏
  • 举报
回复
up
ahao 2003-12-02
  • 打赏
  • 举报
回复
istringstream istr ( (LPCTSTR)s );

string s1;
while( !istr.fail() )
{
getline( istr,s1, ',' );
cout << s1 << ' ';
}
popyyb 2003-12-02
  • 打赏
  • 举报
回复
楼上的大侠,是把","去掉,但是把所有的元素合并在一起了,我的意思是,去掉分割符号,按元素存到一个不定长的string链表或者数组中,谢谢

如: string="aa,bbb,d..."

元素一次为: aa bbb d
akiy 2003-12-02
  • 打赏
  • 举报
回复
把q传出来作为返回值
databasesql 2003-12-02
  • 打赏
  • 举报
回复
用strtok这个函数!
akiy 2003-12-02
  • 打赏
  • 举报
回复
void test()
{
CString str;
str="a,b,c,d,e,";
int iLen=str.GetLength();
char *p,*q;
q=(char*)malloc(iLen+20);
p=q;
for(int i=0;i<iLen;i++)
{
if(str.GetAt(i) !=',')
*p++=str.GetAt(i);
}
*p='\0';
AfxMessageBox(q);
}
ahao 2003-12-02
  • 打赏
  • 举报
回复
到底谁的简单明了阿:)
fuxf 2003-12-02
  • 打赏
  • 举报
回复
还是本人的简单明了
fuxf 2003-12-02
  • 打赏
  • 举报
回复
void test()
{
CString str,result[N],*temparry;//N为常数
str="aa,baaa,aaac,d,e,";
temparry=reault;
int iLen=str.GetLength();
int i=0,j=0;
while(i<iLen)
{
if(str.GetAt(i) !=',')
i++;
else
{*temparry++=str.mid(j,i-j);
j=i+1;i++;}

}
}

16,551

社区成员

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

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

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