资源描述:
《mfc中几个有用的字符串操作函数》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、MFC中几个有用的字符串操作函数MFC中有几个字符串操作函数很有用,但有的在MSDN中都查不到,因为MSDN没有公布。下面我们来看看它们的用法和声明及定义等。//功能:格式化字符串//参数:rString-输出参数,格式化后的字符串将保存在此字符串中//nIDS-将进行替换操作的格式字符串的资源ID//lpsz1-指向将替换格式字符串中“%1”字符的字符串//lpsz2-指向将替换格式字符串中“%2”字符的字符串voidAFXAPIAfxFormatString1(CString&rString,UINTnIDS,LPCTSTRlpsz1);voidAFXAPIAfxFo
2、rmatString2(CString&rString,UINTnIDS,LPCTSTRlpsz1,LPCTSTRlpsz2);//ImplementationstringhelpersvoidAFXAPIAfxFormatStrings(CString&rString,UINTnIDS,LPCTSTRconst*rglpsz,intnString);voidAFXAPIAfxFormatStrings(CString&rString,LPCTSTRlpszFormat,LPCTSTRconst*rglpsz,intnString);//功能:获取子字符串//参数:rSt
3、ring-输出参数,保存子字符串//lpszFullString-源字符串//iSubString-子字符串索引,从0开始计数//chSep-子字符串间的分隔字符,默认为''BOOLAFXAPIAfxExtractSubString(CString&rString,LPCTSTRlpszFullString,intiSubString,TCHARchSep='');/////////////////////////////////////////////////////////////////////////////////////////////////////
4、/Stringsinformat".....%1....%2...."etc.voidAFXAPIAfxFormatStrings(CString&rString,UINTnIDS,LPCTSTRconst*rglpsz,intnString){TCHARszFormat[256];if(!AfxLoadString(nIDS,szFormat)!=0){TRACE1("Error:failedtoloadAfxFormatStringstring0x%04x.",nIDS);ASSERT(FALSE);return;}AfxFormatStrings(rString
5、,szFormat,rglpsz,nString);}voidAFXAPIAfxFormatStrings(CString&rString,LPCTSTRlpszFormat,LPCTSTRconst*rglpsz,intnString){//计算结果字符串的长度intnTotalLen=0;LPCTSTRpchSrc=lpszFormat;while(*pchSrc!=' '){if(pchSrc[0]=='%'&&((pchSrc[1]>='0'&&pchSrc[1]<='9')
6、
7、(pchSrc[1]>='A'&&pchSrc[1]<='Z'))){//%Acom
8、esafter%9--we'llneeditsomedayinti;if(pchSrc[1]>'9')i=9+(pchSrc[1]-'A');elsei=pchSrc[1]-'1';pchSrc+=2;if(i>=nString)++nTotalLen;elseif(rglpsz[i]!=NULL)nTotalLen+=lstrlen(rglpsz[i]);}else{if(_istlead(*pchSrc))++nTotalLen,++pchSrc;++pchSrc;++nTotalLen;}}pchSrc=lpszFormat;LPTSTRpchDest=rStrin
9、g.GetBuffer(nTotalLen);while(*pchSrc!=' '){if(pchSrc[0]=='%'&&((pchSrc[1]>='0'&&pchSrc[1]<='9')
10、
11、(pchSrc[1]>='A'&&pchSrc[1]<='Z'))){//%Acomesafter%9--we'llneeditsomedayinti;if(pchSrc[1]>'9')i=9+(pchSrc[1]-'A');elsei=pchSrc[1]-'1';pchSrc+=2;if(i>=nString){TRACE1("Er