迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

如何在 C++ 中忽略大小写的比较两个字符串

作者:迹忆客 最近更新:2023/04/08 浏览次数:

本文将介绍如何在 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!

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便