Case insensitive string comparison C++

This question already has an answer here:

  • Case-insensitive string comparison in C++ [closed] 31 answers

  • strncasecmp

    The strcasecmp() function performs a byte-by-byte comparison of the strings s1 and s2, ignoring the case of the characters. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

    The strncasecmp() function is similar, except that it compares no more than n bytes of s1 and s2...


    usually what I do is just compare a lower-cased version of the string in question, like:

    if (foo.make_this_lowercase_somehow() == "stack overflow") {
      // be happy
    }
    

    I believe boost has built-in lowercase conversions, so:

    #include <boost/algorithm/string.hpp>    
    
    if (boost::algorithm::to_lower(str) == "stack overflow") {
      //happy time
    }
    

    why don't you you make everything lower case and then compare?

    tolower()

      int counter = 0;
      char str[]="HeLlO wOrLd.n";
      char c;
      while (str[counter]) {
        c = str[counter];
        str[counter] = tolower(c);
        counter++;
      }
    
      printf("%sn", str);
    
    链接地址: http://www.djcxy.com/p/75182.html

    上一篇: Linq to Sql不区分大小写

    下一篇: 不区分大小写的字符串比较C ++