介绍
本文将介绍如何在 C++ 中比较字符串。.
1. 在 C++ 中使用 strcmp() 函数
C++ String 类内置了用于操作字符串数据的函数。strcmp() 函数是 C 语言库中的一个函数,用于按字典顺序比较两个字符串。.
- 输入字符串必须是 C 风格字符串的字符数组。.
- strcmp() 函数还可以区分大小写地比较字符串。.
int strcmp(const char *str1, const char *str2);该函数根据匹配项返回以下值:
- 如果两个字符串相同,则返回 0。.
- 如果第一个字符串的字符值小于第二个字符串的输入值,则返回 < 0(小于零)。.
- 当比较中的第二个字符串较大时,结果为 > 0(大于零)。.
运行以下代码:
#include <iostream>
#include <string.h>
int main()
{
const char *str_inp1 = "String Match";
const char *str_inp2 = "String Unmatch";
std::cout << "String 1: " << str_inp1 << std::endl;
std::cout << "String 2: " << str_inp2 << std::endl;
if (strcmp(str_inp1, str_inp2) == 0)
std::cout << "\nBoth the input strings are equal." << std::endl;
else
std::cout << "\nThe input strings are not equal." << std::endl;
}这将产生以下输出:
OutputString 1: String Match
String 2: String Unmatch
The input strings are not equal.strcmp(str_inp1, str_inp2) 的结果为 -9。str_inp1 和 str_inp2 的值不同。.
运行以下代码:
#include <iostream>
#include <string.h>
int main()
{
const char *str_inp1 = "String Match";
const char *str_inp2 = "String Match";
std::cout << "String 1: " << str_inp1 << std::endl;
std::cout << "String 2: " << str_inp2 << std::endl;
if (strcmp(str_inp1, str_inp2) == 0)
std::cout << "\nBoth the input strings are equal." << std::endl;
else
std::cout << "\nThe input strings are not equal." << std::endl;
}这将产生以下输出:
Output
String 1: String Match
String 2: String Match
Both the input strings are equal.strcmp(str_inp1, str_inp2) 返回 0。str_inp1 和 str_inp2 的值相同。.
2. 在 C++ 中使用 compare() 函数
C++ 内置了 compare() 函数,可以用来比较两个字符串。.
compare() 函数用于比较两个字符串:
int compare (const string& string-name) const;该函数根据匹配项返回以下值:
- 如果两个字符串相同,则返回 0。.
- 如果第一个字符串的字符值小于第二个字符串的输入值,则返回 < 0(小于零)。.
- 当比较中的第二个字符串较大时,结果为 > 0(大于零)。.
运行以下代码:
#include <iostream>
int main()
{
std::string str_inp1("String Match");
std::string str_inp2("String Match");
std::cout << "String 1: " << str_inp1 << std::endl;
std::cout << "String 2: " << str_inp2 << std::endl;
int res = str_inp1.compare(str_inp2);
if (res == 0)
std::cout << "\nBoth the input strings are equal." << std::endl;
else if (res < 0)
std::cout << "\nString 1 is smaller as compared to String 2." << std::endl;
else
std::cout << "\nString 1 is greater as compared to String 2." << std::endl;
}在这个例子中,str_inp1 和 str_inp2 将通过 compare() 函数进行比较:
Output
String 1: String Match
String 2: String Match
Both the input strings are equal.这两个字符串在词法上完全相同,因此该函数返回 0。.
运行以下代码:
#include <iostream>
int main()
{
std::string str_inp0("String Match");
std::string str_inp1("String Match");
std::string str_inp2("String Unmatch");
std::cout << "String 1: " << str_inp1 << std::endl;
if (str_inp1.compare(str_inp0) == 0)
std::cout << "\nStrings are equal." << std::endl;
else
std::cout << "\nStrings are not equal." << std::endl;
std::cout << "String 2: " << str_inp2 << std::endl;
if (str_inp2.compare(str_inp0) == 0)
std::cout << "\nStrings are equal." << std::endl;
else
std::cout << "\nStrings are not equal." << std::endl;
}在这个例子中,str_inp0 与 str_inp1 进行比较:
Output
String 1: String Match
Strings are equal.然后将 str_inp0 与 str_inp2 进行比较:
Output
String 2: String Unmatch
Strings are not equal.这段代码直接使用 compare() 函数将一个字符串与另一个输入字符串进行比较。.
3. C++中的关系运算符
C++ 关系运算符,例如 ==(等于)和 !=(不等于),在比较字符串时非常有用。.
检查两个值是否相等:
string1 == string2检查两个值是否相等:
string1 != string2示例 1:使用 C++ == 运算符
运行以下代码:
#include <iostream>
int main()
{
std::string str_inp1;
std::string str_inp2;
std::cout << "Enter the String 1:\n";
std::cin >> str_inp1;
std::cout << "Enter the String 2:\n";
std::cin >> str_inp2;
if (str_inp1 == str_inp2)
std::cout << "Strings are equal" << std::endl;
else
std::cout << "Strings are not equal" << std::endl;
}请提供«字符串 1»和«字符串 2»的值:
Enter the String 1:
DigitalOcean
Enter the String 2:
digitalocean
Strings are not equal这段代码使用 == 比较两个字符串。.
示例 2:使用 C++ != 运算符
运行以下代码:
#include <iostream>
int main()
{
std::string str_inp1;
std::string str_inp2;
std::cout << "Enter the String 1:\n";
std::cin >> str_inp1;
std::cout << "Enter the String 2:\n";
std::cin >> str_inp2;
if (str_inp1 != str_inp2)
std::cout << "Strings are not equal" << std::endl;
else
std::cout << "Strings are equal" << std::endl;
}请提供«字符串 1»和«字符串 2»的值:
Enter the String 1:
DigitalOcean
Enter the String 2:
DigitalOcean
Strings are equal该代码使用 != 比较两个字符串。.
结果
在本文中,您学习了 C++ 中的字符串比较方法。其中包括 String strcmp() 函数、内置的 compare() 函数和关系运算符(==,!=)。.









