如何在 C++ 中忽略大小寫的比較兩個字串

Jinku Hu 2023年1月30日 2020年11月24日
  1. 使用 strcasecmp 函式比較兩個忽略大小寫的字串
  2. 使用 strncasecmp 函式比較兩個忽略大小寫的字串
  3. 使用自定義的 toLower 函式和 == 操作符來比較兩個字串,忽略大小寫
如何在 C++ 中忽略大小寫的比較兩個字串

本文將演示如何在 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!
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

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

相關文章 - C++ String