求一个字符串的子字符串的问题!我要取如下每行字符串中的第2个域,取出的值为“1”或“0”。[LCM TEST],   1
       "Red",     1, 1000, S, "AT+ELCM=1", E, "AT"
       "Green",   0,  1000, S, "AT+ELCM=0", E, "AT"
问题简化为:
     如果本行只含1个“,”,  需要得到“,”到行尾之间的非空格字符;
     如果本行含2个以上“,”, 需要得到前2个“,”之间的非空格字符;

解决方案 »

  1.   

    CString test=" fds,fds fd,,s ds"; //原字符串
    CString result_Str;          //结果字符串
    //----计算有几个逗号
    int n=0;
    for(int j=0;j<test.GetLength();j++)
    {
    if (test[j]==',')
    {
    n++;
    }
    } if (n==1)
    {
    int index=test.Find(",",0);
    CString resultstr=test.Right(test.GetLength()-index-1);
                       //---去空格处理
    for(int i=0;i<resultstr.GetLength();i++)
    {
    if(resultstr[i]!=32)
    {
    result_Str+=resultstr[i];
    }
    } } if (n>1)
    {
    int index1=test.Find(",",0); //找到第一个逗号的位置
    CString temp1=test.Right(test.GetLength()-index1-1); //获得第一个逗号位置后边的字符串
    int index2=temp1.Find(",",0);  //找到第二个逗号的位置
    CString temp2=temp1.Left(index2);  //获得第二个逗号前边的字符串
    //--------去空格处理
    for(int i=0;i<temp2.GetLength();i++)
    {
    if(temp2[i]!=32)
    {
    result_Str+=temp2[i];
    }
    }
    }//////////////测试过了
      

  2.   

    TO starytx(某某人) :
        您的方法太烦了。   还是lixiaosan(小三)的好!大家看看MSN上 例子。 ms-help://MS.MSDNQTR.2003FEB.2052/vclib/html/_crt_strtok.2c_.wcstok.2c_._mbstok.htm把其中的2行换为:
    CString stri= "      \"Red\",    1, 1000, S, \"AT+ELCM=1\", E, \"AT\"";
    char seps[]   = " ,";
    就可以了。  为什么换了就出错?
      

  3.   

    CString strLine =  _T("Red", 1, 1000, S, "AT+ELCM=1", E, "AT");
    TCHAR *ss = strLine.GetBufferSetLength(strLine.GetLength()); TCHAR *p;CStringArray arr;strLine.ReleaseBuffer(); for ( p=strtok(ss, ","); p!=NULL; p=strtok(NULL, ",") ){ arr.Add(p);}Then the value of arr[1] is what you want.
      

  4.   

    用CString的FIND和MID,TRIMLEFT方法来提取,strtok()对中文提取不理想,使用时要小心.
    这是我以前封装的一个类,供参考int CptStringList::Split(CptString strData,CptString strSep) 
    {
    this->Clear() ; if(strData.GetLength()>0 && strSep.GetLength()>0)
    {
    int nStart = 0 ;
    int nPos = 0 ; while(1)
    {
    nPos = strData.Find(strSep.c_str(),nStart) ;
    if(nPos>0)
    {
    CptString str = strData.Mid(nStart,nPos-nStart) ;
    this->Add(strData.Mid(nStart,nPos-nStart)) ;
    nStart = nPos + strSep.GetLength() ;
    }
    else
    {
    if(nStart!=strData.GetLength())
    {
    this->Add(strData.Mid(nStart,strData.GetLength()-nStart)) ;
    }

    break ;
    }
    }
    } return this->GetCount() ;
    }
      

  5.   

    我来说几句:
     "Red",    1**********************
    是不是就这么个字符串?后面不去管它了。
    如果前面的""里面肯定没有阿拉伯数字,并且第一个逗号之后肯定非0即1的话,那太好办了你就从前往后查找1,然后从前往后查找0,找这么两下子。分析一下:
    如果都没有---------那是不可能的,是么?
    如果一个找到了,一个没找到--------那也是不可能的,对吧?
    如果两个都找到了(这个字符串中既有1也有0)------看谁的位置靠前就是谁。
      

  6.   

    上面我说错了:
    如果都没有找到---------那是不可能的,是么?
    如果一个找到了,一个没找到--------那是可能的,呵呵,找到谁就是谁,找到0就是0,找到1就是1了,
    如果两个都找到了(这个字符串中既有1也有0)------看谁的位置靠前就是谁。因为CString::find函数找的就是就是第一个字符的位置。呵呵
      

  7.   

    如果不是上述那么多条件那么以下方法可以解决更多情况的问题
    首先find到第一个逗号的位置然后循环
    int d=Find(逗号)
    int n=getlength();
    for(int i=d+1;i<=n;i++)
    {
        从n+1位置开始一个一个的找,找到逗号就
        break;
        找不到就让他循环到底
    }
    取d到i之间的字符串就可以了(CString::Mid)