TCHAR字符串查找&反向查找字符串
C++支持两种字符串,即常规的ANSI编码("字符串")和Unicode编码(L"字符串"),相应的就有两套字符串处理函数,比如:strlen和wcslen,分别用于处理两种字符串。
微软将这两套字符集及其操作进行了统一,通过条件编译(_UNICODE&_MBCS)来控制实际使用的字符集。
- 当没有定义_UNICODE & _MBCS宏时TCHAR = char,_tcslen = strlen,_tcsstr = strstr,_tcsncmp = strncmp
- 当定义了_MBCS宏时TCHAR = char,_tcslen = strlen,_tcsstr = _ mbsstr,_tcsncmp = _mbsnbcmp
- 当定义了_UNICODE宏时,TCHAR = wchar_t , _tcslen = wcslen,_tcsstr = wcsstr,_tcsncmp = wcsncmp
字符串查找
TCHAR *szBuffer=_T("Good morning to you and to you !");
TCHAR *szFind=_T("you");
TCHAR *pFind = _tcsstr(szBuffer, szFind);
字符串反向查找
TCHAR *szBuffer=_T("Good morning to you and to you !");
TCHAR *szFind=_T("you");
TCHAR *pLast = szBuffer + _tcslen(szBuffer);
TCHAR *pFind;
for (; pLast >= szBuffer; pLast--)
{
if (*pLast == *szFind)
{
if (_tcsncmp(pLast, szFind, _tcslen(szFind)) == 0)
{
pLast = pLast;
}
}
}