如何查找字符串中的小字符串个数?
比如在字符串 "abcadfbcaebf"   中 查找 'b'的个数或  "ca"的个数

解决方案 »

  1.   

    另,哪位兄弟有CString 类函数的使用大全、详解一类的?最好是中文的。给个链接啊。MSDN看着费劲
      

  2.   

    CString str;
    str = "abcadfbcaebf";
    int nCount1=0, nCount2=0;
    for(int i=0; i<str.GetLength(); i++)
    {
    if(str[i] == 'b')
    nCount1++;
    }
    while( !str.IsEmpty() )
    {
    int index;
    if( (index = str.Find("ca")) != -1)
    {
    nCount2++;
    str = str.Mid(index+2);
    }
    else
    break;
    }
      

  3.   

    int Substr(CString str,CString sub)
    {
    int nRet= 0, nStart = 0;
    while(-1!=(nStart=str.Find(sub,nStart)))
    {
    nStart+=sub.GetLength();
    ++nRet;
    }
    return nRet;
    }usage:
    int n=Substr("abcadfbcaebf","b");
    int n=Substr("abcadfbcaebf","ca");