VB中有split能把字符串按照一定的符号分割成数组,VC里面的相应的函数是什么?
我的字符串是m_Message="abc,def,ghi,klm"
把它分割好放到数组str_N里面,听帮忙写一下代码,谢谢了

解决方案 »

  1.   

    这是我自己写的,你可以参考,调用方法:
    String2Array(sInputFormat, m_arrType, ';');int String2Array(const CString& s, CStringArray &sa, char chSplitter)
    {
    int nLen=s.GetLength(), nLastPos, nPos;
    bool bContinue;

    sa.RemoveAll();
    nLastPos=0;
    do
    {
    bContinue=false;
    nPos = s.Find(chSplitter, nLastPos);
    if (-1!=nPos)
    {
    sa.Add(s.Mid(nLastPos, nPos-nLastPos));
    nLastPos=nPos+1;
    if (nLastPos != nLen) bContinue=true;
    }
    } while (bContinue);

    if (nLastPos != nLen)
    sa.Add(s.Mid(nLastPos, nLen-nLastPos));

    return sa.GetSize();
    }
      

  2.   

    不会吧?VC没有相应的API吗?什么都要自己写,效率怎么提升呀?
      

  3.   

    CString GetParam(CString& mName)
    {
    int P = mName.Find(",");
    char m_Res[256] ="";
    if (P != -1)
    {
    sprintf(m_Res, "%s", mName);
    m_Res[P] = '\0';
    mName.Delete(0, P + 1);
    mName.TrimLeft();
    mName.TrimRight();
    }
    return m_Res;
    }
      

  4.   

    下面的也可以
    int SplitString(CString & str, TCHAR cTok, CStringArray& aryItem)
    {
    TCHAR* p = str.GetBuffer(0);
    TCHAR* e = p;
    TCHAR cEnd = *e;
    int nCount = 0;
    while(cEnd)
    {
    if(*e == _T('\0'))
    cEnd = *e;
    else if(*e == cTok)
    *e = _T('\0');

    if(*e)
    e++;
    else
    {
    if(*p != _T('\0'))
    {
    aryItem.Add(p);
    nCount++;
    }
    p = ++e;
    }
    }
    return nCount;
    }