如何在 C++ 中忽略大小写的比较两个字符串
-
使用
strcasecmp
函数比较两个忽略大小写的字符串 -
使用
strncasecmp
函数比较两个忽略大小写的字符串 -
使用自定义的
toLower
函数和==
操作符来比较两个字符串,忽略大小写
本文将演示如何在 C++ 中比较两个字符串而忽略字母大小写的多种方法。
使用 strcasecmp
函数比较两个忽略大小写的字符串
strcasecmp
是 C 标准库函数,可以使用 <cstring>
头文件包含在 C++ 源文件中。该函数本身以逐个字节为单位进行操作,并在对应的字符串评估时,返回一个小于或等于或大于 0 的整数。
即,如果忽略大小写情况下两个字符串相等,返回值为 0。在其他情况下,当找到第一个不同的字符时,将其与值比较到其字母位置,并返回相应的结果。
#include <iostream>
#include <string>
#include <cstring>
using std::cout; using std::cin;
using std::endl; using std::string;
int main(){
string text1 = "Hey! Mr. Tambourine man, play a song for me";
string text2 = "hey! Mr. Tambourine man, PLAY a song for me";
string text3 = "Hey! Mrs. Tambourine man, play a song for me";
if (strcasecmp(text1.c_str(), text2.c_str()) == 0) {
cout << "strings: text1 and text2 match." << endl;
} else {
cout << "strings: text1 and text2 don't match!" << endl;
}
if (strcasecmp(text1.c_str(), text3.c_str()) == 0) {
cout << "strings: text1 and text3 match." << endl;
} else {
cout << "strings: text1 and text3 don't match!" << endl;
}
return EXIT_SUCCESS;
}
输出:
strings: text1 and text2 match.
strings: text1 and text3 don't match!
使用 strncasecmp
函数比较两个忽略大小写的字符串
strncasecmp
是一个上述函数的另一个变体,可用于从两个字符串中比较给定数量的字符,并且忽略大小写。该函数采用整数值表示需要从第一个字符开始,取需要比较的最大字符数。下面的例子演示了如何比较两个字符串的前 5 个字符,忽略大小写。
#include <iostream>
#include <string>
#include <cstring>
using std::cout; using std::cin;
using std::endl; using std::string;
constexpr int BYTES_TO_COMPARE = 5;
int main(){
string text1 = "Hey! Mr. Tambourine man, play a song for me";
string text3 = "hey! Mrs. Tambourine man, PLAY a song for me";
if (strncasecmp(text1.c_str(), text3.c_str(), BYTES_TO_COMPARE) == 0) {
printf("The first %d characters of strings: text1 and text3 match.\n", BYTES_TO_COMPARE);
}
return EXIT_SUCCESS;
}
输出:
The first 5 characters of strings: text1 and text3 match.
使用自定义的 toLower
函数和 ==
操作符来比较两个字符串,忽略大小写
另外,我们也可以将字符串转换为相同的情况,然后使用简单的 ==
运算符进行比较。作为一种任意的选择,这个例子提供了用用户定义的函数来降低两个字符串,该函数返回 string
对象。最后,if
语句可以包含比较表达式。
#include <iostream>
#include <string>
#include <cstring>
using std::cout; using std::cin;
using std::endl; using std::string;
std::string toLower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c){ return std::tolower(c); });
return s;
}
int main(){
string text1 = "Hey! Mr. Tambourine man, play a song for me";
string text2 = "Hey! Mr. Tambourine man, play a song for me";
string text3 = "Hey! Mrs. Tambourine man, play a song for me";
if (toLower(text1) == toLower(text2)){
cout << "strings: text1 and text2 match." << endl;
} else {
cout << "strings: text1 and text2 don't match!" << endl;
}
if (toLower(text1) == toLower(text3)){
cout << "strings: text1 and text3 match." << endl;
} else {
cout << "strings: text1 and text3 don't match!" << endl;
}
return EXIT_SUCCESS;
}
输出:
strings: text1 and text2 match.
strings: text1 and text3 don't match!
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn