假设有个字符数组bufPohne
 char bufPohne[2100] = "111;222;333;444";  //字符串数组,里面存储的数字字符以分号隔开另外有数组bufRecv
 char bufRecv[4096];我想bufRecv中的字符如下格式: "The number is:111 The number is:222 The number is:333 The number is:444"
其中111,222,333,444为bufPohne中分离出来的字符串这应该如何处理,希望高手指点  谢谢
  

解决方案 »

  1.   

    用strtok()或者sscanf()分割字符串
      

  2.   

    CString str1="111;222;333;444";for(int i=0;i<str1.GetLength();i++)
    {
    char tmpchar = str1.GetAt(i);
    if((tmpchar==';')||(i==0))
    {
    CString str2;
    if(i==0)
    str2="The number is:"+str1.Mid(i,3);
    else
    str2="The number is:"+str1.Mid(i+1,3);
    MessageBox(str2);
    }
    }
      

  3.   

    谢谢 ,
    不过char bufPohne[2100] = "111;222;333;444";  中的数字字符不一定是定长的。
    比如有可能是char bufPohne[2100] = "111;2222;33;4444444";  
      

  4.   

    用CString类可以简单的实现:
    CSting strBufPohne = bufPohne;
    CString strTmp = _T(";") + strBufPohne;
    strTmp.Replace(_T(";"), _T(" The number is:"));
    然后再把strTmp中的内容填回到bufRecv中
      

  5.   

    运行一下下面的代码看看 char bufPohne[] = _T("111;222;333;444;555;666;777;");
    char bufRecv[4096]; 
    CString cstr1 = (char*)bufPohne;
    CString cstr2;
    int a;
    for(;;)
    {
    a= cstr1.Find(';');
    if( a == -1 )break;
    cstr2 += _T(" The number is:") + cstr1.Left(a);
    cstr1 = cstr1.Right(cstr1.GetLength() - a - 1);
    }
    strcpy((char*)bufRecv,cstr2);
      

  6.   

    对字符串的操作,我也觉得使用CString操作比较简单,里面有好多常用的功能,直接调用就行。