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 ++语言中比较字符串的方法 。
Strings in C++ can be compared using either of the following techniques:
可以使用以下两种技术之一来比较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 );
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.
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()函数比较两个字符串,并根据匹配情况返回以下值:
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.
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
In this article, we have understood various ways to compare strings in C++ Language.
在本文中,我们了解了在C ++语言中比较字符串的各种方法。
翻译自: https://www.journaldev.com/37330/compare-strings-in-c-plus-plus
c ++比较字符串是否相等