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"谢谢!
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"谢谢!
假如有3个%d
str2.Format(str1,str3,i1,i2,i3);
假如有5个
str2.Format(str1,str3,i1,i2,i3,i4,i5);
这样不是很麻烦吗?
我想把所有的i都放在vector 里 然后用个循环就可以全部赋值到str2
不知道怎么写请高手赐教
for(int i=0;i<vec.size();i++)
{
strTmp.Format(vec[i]);
str += strTmp;
str += ",";
}
因为%d,不一定是连在一起的啊
this is %d ... %d ... %d ...
怎么加在一起啊
然后str里面有3个%d: this is %d my %d ok %d abc
按照四楼的办法,你怎么弄出来,谢谢
CString str,strTmp;
for(int i=0;i <vec.size();i++)
{
strTmp.Format("%d",vec[i]);
str += strTmp;
str += ",";
}
this is %d my %d ok %d abc1024,44,12
肯定不符合我的要求哦,我要求的结果:this is 1024 my 44 ok 12 abc
就ok了
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;
}
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;
}
//代码有些长,但肯定能得到正确结果。而且效率也不低
就是每次找str的%d ,然后分段,最后分成的N段中间用i1,i2连接起来不知道有没有更好的