在不将字符串转换为所有大写或所有小写的情况下,在C++中进行不区分大小写的字符串比较的最佳方法是什么?
请指出这些方法是否支持Unicode,并且它们的可移植性如何。
对于大小写敏感的比较,可以使用strcmp()
函数。对于大小写不敏感的比较,可以使用strcmpi()
或者stricmp()
函数。这三个函数都在头文件<string.h>
中。
格式:
int strcmp(const char*,const char*); //for case sensitive
int strcmpi(const char*,const char*); //for case insensitive
使用方法:
string a="apple",b="ApPlE",c="ball";
if(strcmpi(a.c_str(),b.c_str())==0) //(if it is a match it will return 0)
cout<<a<<" and "<<b<<" are the same"<<"\n";
if(strcmpi(a.c_str(),b.c_str()<0)
cout<<a[0]<<" comes before ball "<<b[0]<<", so "<<a<<" comes before "<<b;
输出
apple和ApPlE是相同的
a比b先出现,所以apple比ball先出现
// Case insensitive (could use equivalent _stricmp)
result = _stricmp( string1, string2 );
std::string s1 = string("Hello");
if ( _stricmp(s1.c_str(), "HELLO") == 0)
std::cout << "The string are equals.";
看起来以上的解决方案没有使用比较方法并重新实现了总数,所以这是我的解决方案,希望它对你有用(它运行良好)。
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
string tolow(string a)
{
for(unsigned int i=0;i<a.length();i++)
{
a[i]=tolower(a[i]);
}
return a;
}
int main()
{
string str1,str2;
cin>>str1>>str2;
int temp=tolow(str1).compare(tolow(str2));
if(temp>0)
cout<<1;
else if(temp==0)
cout<<0;
else
cout<<-1;
}
strcmp
:
strcmp
通常无法处理Unicode数据。通常,它甚至不能处理基于字节的Unicode编码,如utf-8,因为strcmp
仅进行逐字节比较,而使用utf-8编码的Unicode代码点可能需要超过1个字节。唯一正确处理的特定Unicode情况是,当使用基于字节的编码的字符串仅包含U+00FF以下的代码点时,逐字节比较就足够了。std::wstring first = L"Test";
std::wstring second = L"TEST";
std::wregex pattern(first, std::wregex::icase);
bool isEqual = std::regex_match(second, pattern);
错误:请求将'const char[5]'转换为非标量类型'std::wstring {aka std :: basic_string<wchar_t>}'
。 - Deqing#include <iostream>
struct iequal
{
bool operator()(int c1, int c2) const
{
// case insensitive comparison of two characters.
return std::toupper(c1) == std::toupper(c2);
}
};
bool iequals(const std::string& str1, const std::string& str2)
{
// use std::equal() to compare range of characters using the functor above.
return std::equal(str1.begin(), str1.end(), str2.begin(), iequal());
}
int main(void)
{
std::string str_1 = "HELLO";
std::string str_2 = "hello";
if(iequals(str_1,str_2))
{
std::cout<<"String are equal"<<std::endl;
}
else
{
std::cout<<"String are not equal"<<std::endl;
}
return 0;
}
bool insensitive_c_compare(char A, char B){
static char mid_c = ('Z' + 'a') / 2 + 'Z';
static char up2lo = 'A' - 'a'; /// the offset between upper and lowers
if ('a' >= A and A >= 'z' or 'A' >= A and 'Z' >= A)
if ('a' >= B and B >= 'z' or 'A' >= B and 'Z' >= B)
/// check that the character is infact a letter
/// (trying to turn a 3 into an E would not be pretty!)
{
if (A > mid_c and B > mid_c or A < mid_c and B < mid_c)
{
return A == B;
}
else
{
if (A > mid_c)
A = A - 'a' + 'A';
if (B > mid_c)/// convert all uppercase letters to a lowercase ones
B = B - 'a' + 'A';
/// this could be changed to B = B + up2lo;
return A == B;
}
}
}
这个程序可能可以更高效,但是这里提供了一个庞大的版本,包含所有的细节。
虽然不是很便携,但是在我的电脑上运行良好(我对文字不太熟悉,只懂图片)。
for( int i = 0; i < string2.length(); i++)
{
if (string1[i] == string2[i] || int(string1[i]) == int(string2[j])+32 ||int(string1[i]) == int(string2[i])-32)
{
count++;
continue;
}
else
{
break;
}
if(count == string2.length())
{
//then we have a match
}
}
std::stricmp
。否则,请阅读Herb的观点。 - Konrad Rudolphstrcasecmp
不是标准库的一部分,且至少有一个常用编译器中缺失。 - Mark Ransom