在 C++ 中比较字符串的 3 种方法

简介

在本文中,您将学习在C++中比较字符串的方法。

可以使用以下技术之一比较C++中的字符串:

1.String``strcMP()函数 2.内置Compare()函数 3.C++关系运算符(==!=)

1.在C++中使用String``strcmp()函数

C++String内置了String类型的数据操作函数。strcMP()函数是一个C库函数,用于以词典编纂的方式比较两个字符串。

strcMP()语法

  • 输入字符串必须是C样式字符串的char数组。
  • strcMP()也以区分大小写的形式比较字符串。
1int strcmp(const char *str1, const char *str2);

此函数根据匹配大小写返回下列值:

  • 如果两个字符串相同,则返回`0‘。
  • 如果第一个字符串的字符值小于第二个字符串输入的值,则返回<0(小于零)。
  • 当第二个字符串比较大时,结果为>0(大于零)。

strcmp()示例1

运行以下代码:

 1#include <iostream>
 2
 3#include <string.h>
 4
 5int main()
 6{
 7    const char *str_inp1 = "String Match";
 8    const char *str_inp2 = "String Unmatch";
 9
10    std::cout << "String 1: " << str_inp1 << std::endl;
11    std::cout << "String 2: " << str_inp2 << std::endl;
12
13    if (strcmp(str_inp1, str_inp2) == 0)
14        std::cout << "\nBoth the input strings are equal." << std::endl;
15    else
16        std::cout << "\nThe input strings are not equal." << std::endl;
17}

这将生成以下输出:

1[secondary_label Output]
2String 1: String Match
3String 2: String Unmatch
4The input strings are not equal.

strcmp(str_inp1,str_inp2)结果为-9str_inp1str_inp2的取值不同。

strcmp()示例2

运行以下代码:

 1#include <iostream>
 2
 3#include <string.h>
 4
 5int main()
 6{
 7    const char *str_inp1 = "String Match";
 8    const char *str_inp2 = "String Match";
 9
10    std::cout << "String 1: " << str_inp1 << std::endl;
11    std::cout << "String 2: " << str_inp2 << std::endl;
12
13    if (strcmp(str_inp1, str_inp2) == 0)
14        std::cout << "\nBoth the input strings are equal." << std::endl;
15    else
16        std::cout << "\nThe input strings are not equal." << std::endl;
17}

这将生成以下输出:

1[secondary_label Output]
2String 1: String Match
3String 2: String Match
4
5Both the input strings are equal.

strcmp(str_inp1,str_inp2)结果为0str_inp1str_inp2的值相同。

2.在C++中使用compare()函数

C++有一个内置的compare()函数来比较两个字符串。

Compare()语法

函数的作用是:比较两个字符串:

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

此函数根据匹配大小写返回下列值:

  • 如果两个字符串相同,则返回`0‘。
  • 如果第一个字符串的字符值小于第二个字符串输入的值,则返回<0(小于零)。
  • 当第二个字符串比较大时,结果为>0(大于零)。

示例1:使用Compare()

运行以下代码:

 1#include <iostream>
 2
 3int main()
 4{
 5    std::string str_inp1("String Match");
 6    std::string str_inp2("String Match");
 7
 8    std::cout << "String 1: " << str_inp1 << std::endl;
 9    std::cout << "String 2: " << str_inp2 << std::endl;
10
11    int res = str_inp1.compare(str_inp2);
12
13    if (res == 0)
14    	std::cout << "\nBoth the input strings are equal." << std::endl;
15    else if (res < 0)
16    	std::cout << "\nString 1 is smaller as compared to String 2." << std::endl;
17    else
18    	std::cout << "\nString 1 is greater as compared to String 2." << std::endl;
19}

在本例中,将str_inp1str_inp2Compare()进行比较:

1[secondary_label Output]
2String 1: String Match
3String 2: String Match
4
5Both the input strings are equal.

这两个字符串在词典顺序上是相同的,因此该函数返回‘0’。

示例2:使用Compare()

运行以下代码:

 1#include <iostream>
 2
 3int main()
 4{
 5    std::string str_inp0("String Match");
 6    std::string str_inp1("String Match");
 7    std::string str_inp2("String Unmatch");
 8
 9    std::cout << "String 1: " << str_inp1 << std::endl;
10
11    if (str_inp1.compare(str_inp0) == 0)
12        std::cout << "\nStrings are equal." << std::endl;
13    else
14        std::cout << "\nStrings are not equal." << std::endl;
15
16    std::cout << "String 2: " << str_inp2 << std::endl;
17
18    if (str_inp2.compare(str_inp0) == 0)
19        std::cout << "\nStrings are equal." << std::endl;
20    else
21        std::cout << "\nStrings are not equal." << std::endl;
22}

在本例中,将str_inp0str_inp1进行比较:

1[secondary_label Output]
2String 1: String Match
3Strings are equal.

然后将str_inp0str_inp2进行比较:

1[secondary_label Output]
2String 2: String Unmatch
3Strings are not equal.

这段代码直接将一个字符串与另一个输入字符串进行比较,以获得Compare()函数。

3. C++中的关系运算符

C++关系运算符,如==(双等号)和!=(不等于),在字符串比较中很有帮助。

关系运算符语法

检查两个值是否相等:

1string1 == string2

检查两个值是否不相等:

1string1 != string2

示例1:使用C++==运算符

运行以下代码:

 1#include <iostream>
 2
 3int main()
 4{
 5    std::string str_inp1;
 6    std::string str_inp2;
 7
 8    std::cout << "Enter the String 1:\n";
 9    std::cin >> str_inp1;
10    std::cout << "Enter the String 2:\n";
11    std::cin >> str_inp2;
12
13    if (str_inp1 == str_inp2)
14    	std::cout << "Strings are equal" << std::endl;
15    else
16    	std::cout << "Strings are not equal" << std::endl;
17}

提供字符串1字符串2的值:

1Enter the String 1:
2DigitalOcean
3Enter the String 2:
4digitalocean
5Strings are not equal

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

示例2:使用C++!=运算符

运行以下代码:

 1#include <iostream>
 2
 3int main()
 4{
 5    std::string str_inp1;
 6    std::string str_inp2;
 7
 8    std::cout << "Enter the String 1:\n";
 9    std::cin >> str_inp1;
10    std::cout << "Enter the String 2:\n";
11    std::cin >> str_inp2;
12
13    if (str_inp1 != str_inp2)
14    	std::cout << "Strings are not equal" << std::endl;
15    else
16    	std::cout << "Strings are equal" << std::endl;
17}

提供字符串1字符串2的值:

1Enter the String 1:
2DigitalOcean
3Enter the String 2:
4DigitalOcean
5Strings are equal

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

结论

在本文中,您学习了在C++中比较字符串的方法。这包括String‘SstrcMP()函数,内置的Compare()函数,以及关系运算符(==!=)。

通过更多内容继续学习C++tutorials.

Published At
Categories with 技术
Tagged with
comments powered by Disqus