不区分大小写的字符串比较 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9182912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Case insensitive string comparison C++
提问by CoffeeRain
I know there are ways to do case ignore comparison that involve iterating through strings or one good oneon SO needs another library. I need to put this on other computers that might not have it installed. Is there a way to use the standard libraries to do this? Right now I am just doing...
我知道有一些方法可以进行大小写忽略比较,涉及遍历字符串或SO 上的一个好的字符串需要另一个库。我需要把它放在其他可能没有安装它的计算机上。有没有办法使用标准库来做到这一点?现在我只是在做...
if (foo == "Bar" || foo == "bar")
{
cout << "foo is bar" << endl;
}
else if (foo == "Stack Overflow" || foo == "stack Overflow" || foo == "Stack overflow" || foo == "etc.")
{
cout << "I am too lazy to do the whole thing..." << endl;
}
This could drastically improve the readability and usability of my code. Thanks for reading this far.
这可以大大提高我的代码的可读性和可用性。感谢您阅读到这里。
回答by
The
strcasecmp()
function performs a byte-by-byte comparison of the strings s1and s2, ignoring the case of the characters. It returns an integer less than, equal to, or greater than zero if s1is 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 nbytes of s1and s2...
该
strcasecmp()
函数对字符串s1和s2执行逐字节比较,忽略字符的大小写。如果发现s1分别小于、匹配或大于s2,则它返回一个小于、等于或大于零的整数。的
strncasecmp()
功能是相似的,不同之处在于它比较不超过Ñ字节的S1和S2...
回答by Greg Humphreys
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:
我相信 boost 有内置的小写转换,所以:
#include <boost/algorithm/string.hpp>
if (boost::algorithm::to_lower(str) == "stack overflow") {
//happy time
}
回答by whtlnv
回答by Praetorian
You can write a simple function to convert the existing string to lower case as follows:
您可以编写一个简单的函数将现有字符串转换为小写,如下所示:
#include <string>
#include <ctype.h>
#include <algorithm>
#include <iterator>
#include <iostream>
std::string make_lowercase( const std::string& in )
{
std::string out;
std::transform( in.begin(), in.end(), std::back_inserter( out ), ::tolower );
return out;
}
int main()
{
if( make_lowercase( "Hello, World!" ) == std::string( "hello, world!" ) ) {
std::cout << "match found" << std::endl;
}
return 0;
}
回答by davideanastasia
I just wrote this, maybe it can be useful to somebody:
我刚刚写了这个,也许它对某人有用:
int charDiff(char c1, char c2)
{
if ( tolower(c1) < tolower(c2) ) return -1;
if ( tolower(c1) == tolower(c2) ) return 0;
return 1;
}
int stringCompare(const string& str1, const string& str2)
{
int diff = 0;
int size = std::min(str1.size(), str2.size());
for (size_t idx = 0; idx < size && diff == 0; ++idx)
{
diff += charDiff(str1[idx], str2[idx]);
}
if ( diff != 0 ) return diff;
if ( str2.length() == str1.length() ) return 0;
if ( str2.length() > str1.length() ) return 1;
return -1;
}