在C++中比较字符串的三种方式

介绍

在这篇文章中,你将学习如何在C++中比较字符串的方法。

在C++中,可以使用以下技术来比较字符串:
1. 使用字符串的比较运算符进行比较。
2. 使用字符串的成员函数compare()进行比较。
3. 使用库函数strcmp()进行比较。
4. 使用库函数strcoll()进行比较。(可进行本地化的字符串比较)

    1. 字符串strcmp()函数

 

    1. 内置的compare()函数

 

    C++关系运算符(==,!=)

在C++中使用字符串strcmp()函数。

C++字符串具有系统内置的操作字符串数据的函数。strcmp()函数是一个C库函数,用于按字典顺序比较两个字符串。

strcmp() 语法

  • The input string has to be a char array of C-style String.
  • The strcmp() compares the strings in a case-sensitive form as well.
int strcmp(const char *str1, const char *str2);

根据匹配的情况,此函数返回以下值。

  • Returns 0 if both the strings are the same.
  • Returns < 0 (less than zero) if the value of the character of the first string is smaller as compared to the second string input.
  • Results out to be > 0 (greater than zero) when the second string is greater in comparison.

strcmp()示例1

运行以下代码:

#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;
}

这将会产生以下的输出结果。

Output

String 1: String Match String 2: String Unmatch The input strings are not equal.

strcmp(str_inp1, str_inp2)的结果是-9。str_inp1和str_inp2的值是不同的。

strcmp()示例2

strcmp()函数用于比较两个字符串。下面是strcmp()的示例用法:

“`c++
#include
#include

int main() {
char str1[15], str2[15];
int ret;

strcpy(str1, “abcd”);
strcpy(str2, “wxyz”);

ret = strcmp(str1, str2);

if(ret < 0) { printf(“str1 小于 str2”); } else if(ret > 0) {
printf(“str1 大于 str2”);
} else {
printf(“str1 等于 str2”);
}

return 0;
}
“`

该代码段通过strcmp()函数比较了两个字符串str1和str2。如果str1小于str2,则输出“str1 小于 str2”,如果str1大于str2,则输出“str1 大于 str2”,如果两个字符串相等,则输出“str1 等于 str2”。

运行以下代码:

#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() 函数用于比较两个字符串。

比较(Syntax)函数的语法

compare() 函数用于比较两个字符串。

int compare (const string& string-name) const;

该函数根据匹配的情况返回以下值:

  • Returns 0 if both the strings are the same.
  • Returns < 0 (less than zero) if the value of the character of the first string is smaller as compared to the second string input.
  • Results out to be > 0 (greater than zero) when the second string is greater in comparison.

例1:使用compare()函数

在中文中,”Using compare()函数”可以翻译为 “使用compare()函数”。

运行以下代码:

#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;
}

在这个例子中,使用compare()函数对str_inp1和str_inp2进行比较。

Output

String 1: String Match String 2: String Match Both the input strings are equal.

由于两个字符串在字典上是相同的,因此该函数返回0。

例子2:使用compare()

运行下面的代码:

#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:
Silicon Cloud
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:
Silicon Cloud
Enter the String 2:
Silicon Cloud
Strings are equal

代码将使用!=来比较两个字符串。

结论

在这篇文章中,你学会了在C++中比较字符串的方法。其中包括String的strcmp()函数、内置的compare()函数和关系运算符(==、!=)。

继续通过更多的C++教程来深入学习。

发表回复 0

Your email address will not be published. Required fields are marked *