关于cstring的一个简单问题

oktsl 2009-08-14 03:29:27
cstring str1="this is %s %d operation"
cstring str2;
int i=5;
cstring str3=tom's
str2怎么format为str1 和i
就是说str1是固定的(不能改变,可能有多个%d或%s),我们把str1读出来,然后替换其中的%d %s,然后format为str2
结果为str2="this is tom's 5 operation"

谢谢!
...全文
129 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
oktsl 2009-08-18
  • 打赏
  • 举报
回复
你用的for循环多余了 str2 += str1.Mid(nLastIndex,nCurIndex-nLastIndex); 这个就OK
vcmfcjavabbs 2009-08-14
  • 打赏
  • 举报
回复
快复制我的代码试试吧,然后好好睡一觉
vcmfcjavabbs 2009-08-14
  • 打赏
  • 举报
回复
没有更好的了。代码长,并不表示需要执行的时间也长。
oktsl 2009-08-14
  • 打赏
  • 举报
回复
其实我也有方法解决的
就是每次找str的%d ,然后分段,最后分成的N段中间用i1,i2连接起来

不知道有没有更好的
vcmfcjavabbs 2009-08-14
  • 打赏
  • 举报
回复
//对不起了,上面没有考虑你的每个i都不同,所以改一下:

CString str1, str2, str3;
str1 = "abc def %s %d %s %d";
str3 = "xxx";
int i = 123;
int nCurIndex = 0, nLastIndex = 0;
int nLenOfStr1 = str1.GetLength();
while(1){
nCurIndex = str1.Find("%s", nLastIndex);
if(nCurIndex == -1)
nCurIndex = nLenOfStr1;
for(int nIndex = nLastIndex; nIndex < nCurIndex; nIndex ++)
str2 += str1.GetAt(nIndex);
if(nCurIndex == nLenOfStr1)
break;
str2 += str3;
nLastIndex = nCurIndex + 2;
}
str1 = str2;
str2.Empty();
nCurIndex = 0, nLastIndex = 0;
CString strNum;
nLenOfStr1 = str1.GetLength();
while(1){
nCurIndex = str1.Find("%d", nLastIndex);
if(nCurIndex == -1)
nCurIndex = nLenOfStr1;
for(int nIndex = nLastIndex; nIndex < nCurIndex; nIndex ++)
str2 += str1.GetAt(nIndex);
if(nCurIndex == nLenOfStr1)
break;
strNum.Format("%d", i); //这个i改成你的数组元素i1,i2,i3....
str2 += strNum;
nLastIndex = nCurIndex + 2;
}
//代码有些长,但肯定能得到正确结果。而且效率也不低
vcmfcjavabbs 2009-08-14
  • 打赏
  • 举报
回复
CString str1, str2, str3;
str1 = "abc def %s %d %s %d";
str3 = "xxx";
int i = 123;
int nCurIndex = 0, nLastIndex = 0;
int nLenOfStr1 = str1.GetLength();
while(1){
nCurIndex = str1.Find("%s", nLastIndex);
if(nCurIndex == -1)
nCurIndex = nLenOfStr1;
for(int nIndex = nLastIndex; nIndex < nCurIndex; nIndex ++)
str2 += str1.GetAt(nIndex);
if(nCurIndex == nLenOfStr1)
break;
str2 += str3;
nLastIndex = nCurIndex + 2;
}
str1 = str2;
str2.Empty();
nCurIndex = 0, nLastIndex = 0;
CString strNum;
strNum.Format("%d", i);
nLenOfStr1 = str1.GetLength();
while(1){
nCurIndex = str1.Find("%d", nLastIndex);
if(nCurIndex == -1)
nCurIndex = nLenOfStr1;
for(int nIndex = nLastIndex; nIndex < nCurIndex; nIndex ++)
str2 += str1.GetAt(nIndex);
if(nCurIndex == nLenOfStr1)
break;
str2 += strNum;
nLastIndex = nCurIndex + 2;
}
oktsl 2009-08-14
  • 打赏
  • 举报
回复
还不行的~~~~~~~
郁闷了~
bragi523 2009-08-14
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 bragi523 的回复:]
引用 10 楼 bohut 的回复:
笔误!
CString str,strTmp;
for(int i=0;i <vec.size();i++)
{
  strTmp.Format("%d",vec[i]);
  str += strTmp;
  str += ",";
}



然后str2.Format(str1,str);
就ok了
[/Quote]

哈哈
理解错了啊
没什么太好的办法
bragi523 2009-08-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 bohut 的回复:]
笔误!
CString str,strTmp;
for(int i=0;i <vec.size();i++)
{
  strTmp.Format("%d",vec[i]);
  str += strTmp;
  str += ",";
}
[/Quote]


然后str2.Format(str1,str);
就ok了
oktsl 2009-08-14
  • 打赏
  • 举报
回复
楼上的,不要生气哈
写清楚点哦,我很笨的~~~~~~ 哈哈
bohut 2009-08-14
  • 打赏
  • 举报
回复
ft!
oktsl 2009-08-14
  • 打赏
  • 举报
回复
你这样不是这样的结果吗?
this is %d my %d ok %d abc1024,44,12
肯定不符合我的要求哦,我要求的结果:this is 1024 my 44 ok 12 abc
bohut 2009-08-14
  • 打赏
  • 举报
回复
笔误!
CString str,strTmp;
for(int i=0;i <vec.size();i++)
{
strTmp.Format("%d",vec[i]);
str += strTmp;
str += ",";
}
oktsl 2009-08-14
  • 打赏
  • 举报
回复
楼上的,不知道你没看懂我说的,还是我没看懂你说的

vector vec里面有很多i ,比如有3个:1024, 44, 12
然后str里面有3个%d: this is %d my %d ok %d abc
按照四楼的办法,你怎么弄出来,谢谢
chogimoga 2009-08-14
  • 打赏
  • 举报
回复
我晕,置什么换?你把整个CString换了就行了

按照4楼的做法就行了
oktsl 2009-08-14
  • 打赏
  • 举报
回复
因为有很多str 从一个文本里读出来,这个str可能只有1个%d 那个可能有4个%d,每次情况都不同

每次从文本里读出对应的str就要置换其中的%d ,就是这个意思,%d的值已经存在vector里,每次存的个数当然也不一样,但是都等于str里面%d的个数

现在就是要置换出来

oktsl 2009-08-14
  • 打赏
  • 举报
回复
用+肯定不对哦
因为%d,不一定是连在一起的啊
this is %d ... %d ... %d ...
怎么加在一起啊
路人乙2019 2009-08-14
  • 打赏
  • 举报
回复
既然是不定的,那个为何不根据具体情况format.用个数组保存每个代替符号不是不可以,但你得判断出是干什么的,顺序不能变.
bohut 2009-08-14
  • 打赏
  • 举报
回复
CString str,strTmp;
for(int i=0;i<vec.size();i++)
{
strTmp.Format(vec[i]);
str += strTmp;
str += ",";
}
oktsl 2009-08-14
  • 打赏
  • 举报
回复
关键问题是不定的哦
假如有3个%d
str2.Format(str1,str3,i1,i2,i3);
假如有5个
str2.Format(str1,str3,i1,i2,i3,i4,i5);
这样不是很麻烦吗?
我想把所有的i都放在vector 里 然后用个循环就可以全部赋值到str2
不知道怎么写

请高手赐教
加载更多回复(2)

16,472

社区成员

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

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

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