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"谢谢!

解决方案 »

  1.   

    关键问题是不定的哦
    假如有3个%d
    str2.Format(str1,str3,i1,i2,i3);
    假如有5个
    str2.Format(str1,str3,i1,i2,i3,i4,i5);
    这样不是很麻烦吗?
    我想把所有的i都放在vector 里 然后用个循环就可以全部赋值到str2
    不知道怎么写请高手赐教
      

  2.   

    CString str,strTmp;
    for(int i=0;i<vec.size();i++)
    {
       strTmp.Format(vec[i]);
       str += strTmp;
       str += ",";
    }
      

  3.   

    既然是不定的,那个为何不根据具体情况format.用个数组保存每个代替符号不是不可以,但你得判断出是干什么的,顺序不能变.
      

  4.   

    用+肯定不对哦
    因为%d,不一定是连在一起的啊
    this is %d ... %d ... %d ...
    怎么加在一起啊
      

  5.   

    因为有很多str 从一个文本里读出来,这个str可能只有1个%d 那个可能有4个%d,每次情况都不同每次从文本里读出对应的str就要置换其中的%d ,就是这个意思,%d的值已经存在vector里,每次存的个数当然也不一样,但是都等于str里面%d的个数现在就是要置换出来
      

  6.   

    我晕,置什么换?你把整个CString换了就行了按照4楼的做法就行了 
      

  7.   

    楼上的,不知道你没看懂我说的,还是我没看懂你说的vector vec里面有很多i ,比如有3个:1024, 44, 12
    然后str里面有3个%d: this is %d my %d ok %d abc
    按照四楼的办法,你怎么弄出来,谢谢
      

  8.   

    笔误!
    CString str,strTmp; 
    for(int i=0;i <vec.size();i++) 

      strTmp.Format("%d",vec[i]); 
      str += strTmp; 
      str += ","; 
    }
      

  9.   

    你这样不是这样的结果吗?
    this is %d my %d ok %d abc1024,44,12 
    肯定不符合我的要求哦,我要求的结果:this is 1024 my 44 ok 12 abc
      

  10.   

    然后str2.Format(str1,str);
    就ok了
      

  11.   

      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;
      }
       
      

  12.   

     //对不起了,上面没有考虑你的每个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; 
      } 
      //代码有些长,但肯定能得到正确结果。而且效率也不低
      

  13.   

    其实我也有方法解决的
    就是每次找str的%d ,然后分段,最后分成的N段中间用i1,i2连接起来不知道有没有更好的
      

  14.   

    你用的for循环多余了 str2 += str1.Mid(nLastIndex,nCurIndex-nLastIndex); 这个就OK