c ++比较字符串是否相等_在C ++中比较字符串的3种方法

c ++比较字符串是否相等

In this tutorial, we’ll learn methods to compare strings in C++. Consider a scenario wherein you are required to enter your name and password to login to a particular website. In such cases, at the back end, we need to build and script functions to check and compare the input string (login details) with the string stored in the data base.

在本教程中,我们将学习在C ++中比较字符串的方法。 考虑一种方案,其中要求您输入名称和密码才能登录到特定网站。 在这种情况下,在后端,我们需要构建和编写脚本函数,以检查输入字符串(登录详细信息)并将其与数据库中存储的字符串进行比较。

So, relating it here, in this article, will be understanding the Ways to compare string in C++ language.

因此,在本文中与此相关的将是理解在C ++语言中比较字符串方法



在C ++中比较字符串的技术 (Techniques to Compare Strings in C++)

Strings in C++ can be compared using either of the following techniques:

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

  • String strcmp() function

    字符串strcmp()函数
  • In-built compare() function

    内置compare()函数
  • C++ Relational Operators ( ‘==’ , ‘!=’ )

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


1. C ++中的字符串strcmp()函数 (1. String strcmp() function in C++)

C++ String has got in-built functions to manipulate and deal with data of string type. In order to compare two strings, we can use String’s strcmp() function.

C ++ String具有内置函数来处理和处理字符串类型的数据。 为了比较两个字符串,我们可以使用String的strcmp()函数。

The strcmp() function is a C library function used to compare two strings in a lexicographical manner.

strcmp()函数是C库函数,用于按字典顺序比较两个字符串。

Syntax:

句法:


int strcmp ( const char * str1, const char * str2 );
  • The function returns 0 if both the strings are equal or the same.

    如果两个字符串相等或相同, 函数返回0
  • The input string has to be a char array of C-style string.

    输入字符串必须是C样式字符串的char数组。
  • The strcmp() compares the strings in a case-sensitive form as well.

    strcmp()也以区分大小写的形式比较字符串。

Example 1:

范例1:


#include
using namespace std;
#include
int main() {
   const char *str_inp1="JournalDEV";
   const char *str_inp2="JournalDEv";
   
cout<<"String 1:"<

Output:

输出:


String 1:JournalDEV
String 2:JournalDEv
The input strings are not equal.

Example 2:

范例2:


#include
using namespace std;
#include
int main() {
   const char *str_inp1="Python";
   const char *str_inp2="Python";
   
cout<<"String 1:"<

Output:

输出:


String 1:Python
String 2:Python

Both the input strings are equal.


2. C ++中的compare()函数 (2. The compare() function in C++)

C++ has in-built compare() function in order to compare two strings efficiently.

C ++具有内置的compare()函数,以便有效地比较两个字符串。

The compare() function compares two strings and returns the following values according to the matching cases:

compare()函数比较两个字符串,并根据匹配情况返回以下值:

  • Returns 0, if both the strings are the same.

    如果两个字符串相同,则返回0。
  • Returns <0, if the value of the character of the first string is smaller as compared to the second string input.

    如果第一个字符串的字符值小于第二个字符串输入的字符,则返回<0。
  • Results out to be >0, when the second string is greater in comparison.

    当第二个字符串比较大时,结果为> 0。

Syntax:

语法


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

Example 1:

范例1:


#include
using namespace std;

int main() {
   string str_inp1("Python");
   string str_inp2("Python");
   
cout<<"String 1:"<

In the above example, we have compared string 1 with string 2. As clearly seen, both the strings are the same lexicographically, it returns 0.

在上面的示例中,我们将字符串1与字符串2进行了比较。可以清楚地看到,两个字符串在字典上都相同,返回0。

Output:

输出:


String 1:Python
String 2:Python

Both the input strings are equal.

Example 2:

范例2:


#include
using namespace std;

int main() {
   string str_inp1("Python");
   string str_inp2("JournalDEV");
   
cout<<"String 1:"<

In the above snippet of code, we have compared a string with another input string to the compare() function directly.

在上面的代码片段中,我们将一个字符串和另一个输入字符串直接比较到compare()函数。

Output:

输出:


String 1:Python
Strings are equal.

String 2:JournalDEV
Strings are not Equal.


3. C ++中的关系运算符 (3. Relational Operators in C++)

C++ Relational operators such as ‘==’ and ‘!=’ can be useful in the comparison of string at an ease.

C ++关系运算符(例如'=='和'!=')在轻松比较字符串时很有用。

Syntax:

句法:


string1 == string 2
OR
string1 != string2

Example 1: Using C++ ‘==’ operator

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


#include
using namespace std;

int main() {
   string str_inp1;
   string str_inp2;
   
   cout<<"Enter the String 1:\n";
   cin>>str_inp1;
   cout<<"Enter the String 2:\n";
   cin>>str_inp2;
   

    if (str_inp1 == str_inp2) 
        cout <<"Strings are equal"<

Output:

输出:


Enter the String 1:
Python
Enter the String 2:
PythoN
Strings are not equal

Example 2: Using C++ ‘!=’ operator

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


#include
using namespace std;

int main() {
   string str_inp1;
   string str_inp2;
   
   cout<<"Enter the String 1:\n";
   cin>>str_inp1;
   cout<<"Enter the String 2:\n";
   cin>>str_inp2;
   

    if (str_inp1 != str_inp2) 
        cout <<"Strings are not equal"<

Output:

输出:


Enter the String 1:
Python
Enter the String 2:
Python
Strings are equal


结论 (Conclusion)

In this article, we have understood various ways to compare strings in C++ Language.

在本文中,我们了解了在C ++语言中比较字符串的各种方法。



参考资料 (References)

  • The compare() function in C++ – Official Documentation

    C ++中的compare()函数–官方文档

翻译自: https://www.journaldev.com/37330/compare-strings-in-c-plus-plus

c ++比较字符串是否相等

你可能感兴趣的:(字符串,c++,python,人工智能,编程语言)