TCHAR字符串查找&反向查找字符串

C++支持两种字符串,即常规的ANSI编码("字符串")和Unicode编码(L"字符串"),相应的就有两套字符串处理函数,比如:strlen和wcslen,分别用于处理两种字符串。
微软将这两套字符集及其操作进行了统一,通过条件编译(_UNICODE&_MBCS)来控制实际使用的字符集。

  1. 当没有定义_UNICODE & _MBCS宏时TCHAR = char,_tcslen = strlen,_tcsstr = strstr,_tcsncmp = strncmp
  2. 当定义了_MBCS宏时TCHAR = char,_tcslen = strlen,_tcsstr = _ mbsstr,_tcsncmp = _mbsnbcmp
  3. 当定义了_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;
			}
		}
	}

参考资料

  1. Generic-Text Mappings in Tchar.h
  2. strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l
  3. strstr, wcsstr, _mbsstr, _mbsstr_l
  4. strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
posted @ 2017-09-12 13:47  ,,,沙子,,,  阅读(952)  评论(0编辑  收藏  举报