关于Split函数和AfxExtractSubString函数(等同于VB的Split函数)

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.分隔只能使用字符,不能使用字符串。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值