Compares two strings (case insensitive)

长平狐 发布于 2012/08/28 16:38
阅读 26
收藏 0

#pragma once
#include <string>
using namespace std;

class ci_char_traits :
 public std::char_traits<char>
{
public:
 static bool eq(char c1,char c2)
 {
  return toupper(c1)==toupper(c2);
 }

 static bool lt(char c1,char c2)
 {
  return toupper(c1)<toupper(c2);
 }

 static int compare(const char* s1,const char* s2,size_t n)
 {
  return _memicmp(s1,s2,n);
 }

 static const char* find(const char* s,int n,char a)
 {
  while(n-- > 0 && toupper(*s)!=toupper(a))
  {
   ++s;
  }
  return n>0 ? s:0;
 }
};


We can use the ci_char_traits class as thus:

         basic_string<char,ci_char_traits> str1,str2;
 str1="oK";
 str2="ok";
 if(str1==str2)
 {
  cout<<"ok"<<endl;
 }
 if(str1=="ok")
 {
  cout<<"ok"<<endl;
 }
 cout<<str1.c_str()<<endl;
 return 0;


原文链接:http://blog.csdn.net/sheismylife/article/details/1154918
加载中
返回顶部
顶部