關於Split函數和AfxExtractSubString函數(等同於VB的Split函數)

來源:互聯網
上載者:User
VC中SPLIT函數的構造如下:

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

 

或者是:

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

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

VC下的Split函數

熟悉VB代碼的朋友應該都極度喜歡Split函數,因為實在太方便了,自動幫你把字串按照要求拆分,在VC++中微軟隱藏了一個函數,其功效等同與VB中的SPLIT函數,該函數原型如下:

BOOL AfxExtractSubString(CString& rString, LPCTSTR lpszFullString,int iSubString, TCHAR chSep = '/n')

       參數說明:

        rString 得到的字串;lpszFullString 待分割的字串;iSubString 要得到第幾個字串;chSep 個子串之間的分隔字元,預設是斷行符號;

        傳回值為Flase表示iSubString 越界,否則分隔成功

        例如,有一個字串strFullString = "abcd-hgdy-weiuiwu-sdlsk";則有:

        CString strTmp;

        AfxExtractSubString( strTmp, (LPCTSTR)strFullString, 0, '-');//strTmp的內容為abcd

        AfxExtractSubString( strTmp, (LPCTSTR)strFullString, 2, '-');//strTmp的內容為weiuiwu

        感覺蠻好用的,但是有兩個限制:

1.僅僅能在MFC下使用的函數

2.分隔只能使用字元,不能使用字串。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.