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 (); } Or:
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; } //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////// Split function under VCFriends who are familiar with VB code should like the Split function very much, because it is too convenient to automatically Split the string as required. Microsoft hides a function in VC ++, the function is equivalent to the SPLIT function in VB. The function prototype is as follows: BOOL AfxExtractSubString (CString & rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep = '/N ') Parameter description: The string obtained by rstring; the string to be split by lpszfullstring; the number of strings to be obtained by isubstring; the separator between chsep substrings. The default value is press Enter; If the returned value is flase, The isubstring is out of bounds. Otherwise, the separation is successful. For example, if there is a string strfullstring = "ABCD-hgdy-weiuiwu-sdlsk", then there are: Cstring strtmp; Afxextractsubstring (strtmp, (lpctstr) strfullstring, 0, '-'); // The content of strtmp is ABCD. Afxextractsubstring (strtmp, (lpctstr) strfullstring, 2, '-'); // The content of strtmp is weiuiwu It seems quite useful, but there are two restrictions: 1. functions that can only be used in MFC 2. Only characters can be used to separate strings. |