MFC中几个有用的字符串操作函数
发布时间:2006-05-05 19:02:56 来源:VC在线 网友评论 0 条MFC中有几个字符串操作函数很有用,但有的在MSDN中都查不到,因为MSDN没有公布。下面我们来看看它们的用法和声明及定义等。
// 功能 : 格式化字符串
// 参数 : rString - 输出参数,格式化后的字符串将保存在此字符串中
// nIDS - 将进行替换操作的格式字符串的资源ID
// lpsz1 - 指向将替换格式字符串中“%1”字符的字符串
// lpsz2 - 指向将替换格式字符串中“%2”字符的字符串
void AFXAPI AfxFormatString1(CString& rString, UINT nIDS, LPCTSTR lpsz1);
void AFXAPI AfxFormatString2(CString& rString, UINT nIDS, LPCTSTR lpsz1, LPCTSTR lpsz2);
// Implementation string helpers
void AFXAPI AfxFormatStrings(CString& rString, UINT nIDS, LPCTSTR const* rglpsz, int nString);
void AFXAPI AfxFormatStrings(CString& rString, LPCTSTR lpszFormat, LPCTSTR const* rglpsz, int nString);
// 功能 : 获取子字符串
// 参数 : rString - 输出参数,保存子字符串
// lpszFullString - 源字符串
// iSubString - 子字符串索引,从0开始计数
// chSep - 子字符串间的分隔字符,默认为'/n'
BOOL AFXAPI AfxExtractSubString(CString& rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep = '/n');
/////////////////////////////////////////////////
////////////////////////////////////////////////
///
// Strings in format ".....%1 .... %2 ...." etc. void AFXAPI AfxFormatStrings(CString& rString, UINT nIDS,
LPCTSTR 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);
return;
}
AfxFormatStrings(rString, szFormat, rglpsz, nString);
}
void AFXAPI AfxFormatStrings(CString& rString, LPCTSTR lpszFormat,
LPCTSTR const* rglpsz, int nString)
{
// 计算结果字符串的长度
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] != NULL)
nTotalLen += lstrlen(rglpsz[i]);
}
else
{
if (_istlead(*pchSrc))
++nTotalLen, ++pchSrc;
++pchSrc;
++nTotalLen;
}
}
pchSrc = lpszFormat;
LPTSTR pchDest = rString.GetBuffer(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 requested %d./n", i);
*pchDest++ = '?';
}
else if (rglpsz[i] != NULL)
{
lstrcpy(pchDest, rglpsz[i]);
pchDest += lstrlen(pchDest);
}
}
else
{
if (_istlead(*pchSr
c)) // *pchSrc是否多字节字符的首字节
*pchDest++ = *pchSrc++; // 拷贝首字节
*pchDest++ = *pchSrc++;
}
}
rString.ReleaseBuffer((int)((LPCTSTR)pchDest - (LPCTSTR)rString));
// ReleaseBuffer will assert if we went too far
}
void AFXAPI AfxFormatString1(CString& rString, UINT nIDS, LPCTSTR 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)(lpchEnd - lpszFullString);
ASSERT(nLen >= 0);
memcpy(rString.GetBufferSetLength(nLen), lpszFullString, nLen*sizeof(TCHAR));
return TRUE;
}
/////////////////////////////////////////////////
////////////////////////////////////////////////
///- 推荐阅讯
- Windows Sockets:转换字符串
- C++中的运算符重载函数基础及其值返回状态
- C/C++作用域引申出的编码规范
- 深入理解sizeof
- 细谈C++多态性的“动”与“静”
- VC#2005快速入门之使用do语句
- 文件加密技术一例
- c++面向对象的编程入门篇-----类(class)
- C++远程关机API的学习过程
- 对于c/c++中指针学习的两个绝好的例子
- 阅读排行
- 1.Borland 发布C++ Builder 2006 RAD 环境
- 2.C/C++程序员应聘常见面试题深入剖析
- 3.Visual C++常用数据类型转换详解
- 4.C++中的 static 关键字
- 5.利用VC++实现局域网实时视频传输
- 6.浅谈C/C++内存泄漏及其检测工具
- 7.英国投票否决C++/CLI 微软强攻ISO标准受挫
- 8.VC++下用MSComm控件实现串口通讯
- 9.伪随机数生成及在VC++中的实现
- 10.VC++编程实现对波形数据的频谱分析
- 专题教程
- Windows Server-Windows Server文档-Windows Server新闻-Windows Ser PostgreSQL-PostgreSQL文档-PostgreSQL新闻-PostgreSQL专家
- WebLogic-WebLogic文档-WebLogic新闻-WebLogic专家 FreeBSD-FreeBSD文档-FreeBSD新闻-FreeBSD专家
- Linux-内核 GUI KDE Gnome DNS FTP 安全 安装-Linux专区 Windows-AD IIS ServerCore 虚拟化 安全 HPC-Windows专区
- 大话G游 专题:手机病毒揭密
- ARP攻击防范与解决方案 路由故障处理手册
