Several useful string operation functions in MFC

xiaoxiao2021-03-06  72

// Format: Formatted string // Parameter: RString - Output parameters, formatted strings will be saved in this string // NIDS - the resource ID // lpsz1 of the format string will be replaced A string // LPSZ2 in the "% 1" character in the replacement format string - points to the string of "% 2" characters in the replacement format string "% 2" characters "CSTRING & RSTRING, UINT NIDS, LPCTSTR LPSZ1); Void AFXAPI AFXFORMATSTRING2 (CString & rString, UINT nIDS, LPCTSTR lpsz1, LPCTSTR lpsz2); // Implementation string helpersvoid AFXAPI AfxFormatStrings (CString & rString, UINT nIDS, LPCTSTR const * rglpsz, int nString); void AFXAPI AfxFormatStrings (CString & rString, LPCTSTR lpszFormat, LPCTSTR const * RGLPSZ, INT nString; // Function: Get Subtrinary String // Parameters: RString - Output Parameters, Save Subtrial // LpszfullString - Source string // isubstring - Sub string index, start counting from 0 // CHSEP - Separate characters between substrings, defaults to '/ N'Bool AFXAPI AFXTRACTSUBSTRING (CSTRING & RSTRING, LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTRING, TCHAR Chsep =' / n '); // Strings in format ".....% 1 ....% 2 .... "etc .void AFXAPI AFXFORMATSTRINGS (CSTRING & RSTRING, UINT NIDS, LPCTST R const * rglpsz, int nstring) {tchar szformat [256]; if (! AFXLOADSTRING (NIDS, SZFORMAT)! = 0) {Trace1 ("Error: failed to load afxformatstring string 0x% 04x. / N", NIDS); ASSERT (FALSE);} AFXFORMATSTRINGS (RString, Szformat, RGLPSZ, NSTRING);

void AFXAPI AfxFormatStrings (CString & rString, LPCTSTR lpszFormat, LPCTSTR const * rglpsz, int nString) {// calculate the length of the resulting string int nTotalLen = 0; LPCTSTR pchSrc = lpszFormat; while (! * pchSrc = '/ 0') {if (PCHSRC [0] == '%' && ((PCHSRC [1]> = '0' && Pchsrc [1] <= '9') || (PCHSRC [1]> = 'A' && Pchsrc [1] <= 'Z'))) {//% a comes after% 9 - We'll Need IT Someday Int i; if (PCHSRC [1]> '9') i = 9 (PCHSRC [1] - ' A '); ELSE I = PCHSRC [1] -' 1 '; PCHSRC = 2; if (i> = nstring) NTOTALLEN; ELSE IF (RGLPSZ [i]! = Ntotallen = lstrlen (RGLPSZ [ I]);} else {ife (_ITLEAD (* PCHSRC)) NTOTALLEN, PCHSRC; PCHSRC; NTOTALLEN;}}

PCHSRC = LPSZFORMAT; LPTSTSTBUFFER (NTOTALLEN); While (* pchsrc! = '/ 0') {IF (PCHSRC [0] == '%' && ((PCHSRC [1]> = '0' && PCHSRC [1] <= '9') || (PCHSRC [1]> = 'a' && pchsrc [1] <= 'z'))) {//% a comes after% 9 - We'll NEED IT Someday Int i; IF (PCHSRC [1]> '9') i = 9 (PCHSRC [1] - 'A'); ELSE I = PCHSRC [1] - '1'; PCHSRC = 2; IF I> = nstring) {trace1 ("Error: Illegal string index request% d. / n", i); * PCHDEST = '' ';} else if (rglps [i]! = null) {lstrcpy (PCHDEST, RGLPSZ [i]); PCHDEST = LSTRLEN (PCHDEST);}}} else {if (_Stlead (* pchsrc)) // * PCHSRC is the first word * PCHDest = * PchSrc ; // copy of the first byte * pchDest = * pchSrc ;}} rString.ReleaseBuffer ((int) ((LPCTSTR) pchDest - (LPCTSTR) rString)); // ReleaseBuffer will assert if we went too far} void AFXAPI AFXFORMATSTRING1 (CSTRING & RSTRING, UINT NIDS, LPCTSTSTR LPSZ1) {AFXFORMATSTRINGS (Rstring, NIDS, & LPSZ1, 1);}

void AFXAPI AfxFormatString2 (CString & rString, UINT nIDS, LPCTSTR lpsz1, LPCTSTR lpsz2) {LPCTSTR rglpsz [2]; rglpsz [0] = lpsz1; rglpsz [1] = lpsz2; AfxFormatStrings (rString, nIDS, rglpsz, 2);} BOOL AFXAPI AfxExtractSubString (CString & rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep) {if (lpszFullString == NULL) return FALSE; while (iSubString--) {lpszFullString = _tcschr (lpszFullString, chSep); if (lpszFullString == NULL) { rString.Empty (); // return empty string as well return FALSE;} lpszFullString ; // point past the separator} LPCTSTR lpchEnd = _tcschr (lpszFullString, chSep);? int nLen = (lpchEnd == NULL) lstrlen (lpszFullString) : (int); assert (nlen> = 0); Memcpy (RString.getBuffersetLength (Nlen), LPSZFULLSTRING, NLEN * SIZEOF (TCHAR)); Return true;}

转载请注明原文地址:https://www.9cbs.com/read-92371.html

New Post(0)