使用C和C++标准函数忽略大小写比较字符串

来源:互联网 发布:人力资源规划 数据分析 编辑:程序博客网 时间:2024/06/05 06:56

环境:win32命令行程序, unicode编码

功能:比较字符串,忽略大小写

限制:windows平台,不使用MFC库函数



#include <string>
using namespace std;

#include <string.h>


BOOL Getfilesuffix(const TCHAR* pFilePath)
{
 wstring str(pFilePath);
 str = str.substr(str.find_last_of('\\') + 1);        //找到路径中的文件名
 str = str.substr(str.find_first_of('.') + 1);        //找到文件名中的后缀,从第一个字符  '.' 开始计算
 
 if (0 != _wcsicmp(L"Text", str.c_str()))
  return FALSE;

 return TRUE;
}


int main()

{

      WCHAR* pName = "c:\haha\xixi.text";

      if(TRUE == Getfilesuffix(pName))

      {

           printf("The file is correct!\n");

      }

     

      return 0;

}


说明:其中_wcsicmp是C的函数,wstring 是c++的函数,因为wstring 本身没有忽略大小写的比较,MFC倒是有,但是此例子用不起来,所以

只好结合C函数来处理。




原创粉丝点击