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