In this C++ tutorial, you will learn how to check whether two strings are equal using the Equal-to == operator and the std::string::compare() function. You will also see how string comparison behaves with uppercase and lowercase letters, how to compare part of a string, and when strcmp() is appropriate for C-style strings.
How C++ String Equality Works
Two std::string objects are equal when they contain the same sequence of characters in the same order. String equality in C++ is case-sensitive, so "hello" and "Hello" are not equal.
For ordinary equality checks between two std::string objects, the == operator is the most direct choice. The compare() member function is useful when you also need ordering information or want to compare selected portions of strings.
Check if C++ Strings Are Equal Using the == Operator
The Equal-to operator == compares two strings and produces a Boolean result. It returns true when the strings contain the same characters and false otherwise.
The basic form is:
str1 == str2
In the following two example programs, we initialize two strings with some values and check if these two strings are equal using equal to operator.
First let us take two strings with different values.
C++ Program
#include <iostream>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "hi";
if (str1 == str2) {
cout << "The two strings are equal.";
} else {
cout << "The two strings are not equal.";
}
}
Output
The two strings are not equal.
As the two strings are not equal, str1 == str2 returned false. So, else block is executed.
Now, let us provide same values for both the strings and check the output.
C++ Program
#include <iostream>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "hello";
if (str1 == str2) {
cout << "The two strings are equal.";
} else {
cout << "The two strings are not equal.";
}
}
Output
The two strings are equal.
As the two strings have same value, str1 == str2 returned true and the if block is executed.
When writing new code that uses std::string, include the <string> header explicitly. The examples above are kept as originally written.
Check if C++ Strings Are Equal Using std::string::compare()
compare() is a member function of std::string. It compares the calling string with another string and returns an integer that describes their relative order.
0means the two compared strings are equal.- A value less than
0means the calling string compares before the other string. - A value greater than
0means the calling string compares after the other string.
Do not depend on a particular negative or positive number. For an equality check, test whether the result is exactly 0.
The syntax of compare() function is
str1.compare(str2)
where str1 and str2 are strings.
In the following two example programs, we initialize two strings with some values and check if these two strings are equal using compare() function.
First let us take two strings with different values, and check if they are equal.
C++ Program
#include <iostream>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "hi";
if (str1.compare(str2) == 0) {
cout << "The two strings are equal.";
} else {
cout << "The two strings are not equal.";
}
}
Output
The two strings are not equal.
As the two strings are not equal, str1.compare(str2) returned a non-zero value. So, else block is executed.
Now, let us provide same values for both the strings and check the output.
C++ Program
#include <iostream>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "hello";
if (str1.compare(str2) == 0) {
cout << "The two strings are equal.";
} else {
cout << "The two strings are not equal.";
}
}
Output
The two strings are equal.
Here, str1.compare(str2) returns 0, so the condition str1.compare(str2) == 0 is true and the if block is executed.
C++ String == vs compare()
Both approaches can test equality, but they communicate slightly different intent.
| Method | Result | Best suited for |
|---|---|---|
str1 == str2 | true or false | Direct equality checks |
str1.compare(str2) | Zero, negative, or positive integer | Equality plus lexicographical ordering or partial comparisons |
If the only question is whether two std::string values are equal, == is usually clearer. Use compare() when its additional comparison forms are useful.
C++ String Equality Is Case-Sensitive
The built-in std::string equality operations compare characters as stored. Therefore, strings that differ only by letter case are not equal.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hello";
string str2 = "hello";
if (str1 == str2) {
cout << "The two strings are equal.";
} else {
cout << "The two strings are not equal.";
}
return 0;
}
Output
The two strings are not equal.
If your application needs case-insensitive comparison, define that rule explicitly. One simple approach for ASCII-oriented input is to compare lowercase forms of both strings.
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
string toLower(string value) {
transform(value.begin(), value.end(), value.begin(),
[](unsigned char ch) {
return static_cast<char>(tolower(ch));
});
return value;
}
int main() {
string str1 = "Hello";
string str2 = "hello";
if (toLower(str1) == toLower(str2)) {
cout << "The two strings are equal ignoring case.";
} else {
cout << "The two strings are not equal.";
}
return 0;
}
Output
The two strings are equal ignoring case.
This example is suitable for simple character data. Case conversion for international text can require locale- or Unicode-aware handling beyond basic std::tolower.
Compare a Substring with std::string::compare()
std::string::compare() has overloads that can compare only part of a string. The form below compares count characters beginning at pos with another string.
str.compare(pos, count, other)
For example, the first five characters of "hello world" can be compared with "hello" without first creating a separate substring.
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "hello world";
string word = "hello";
if (text.compare(0, 5, word) == 0) {
cout << "The first five characters are equal.";
}
return 0;
}
Output
The first five characters are equal.
Compare the First N Characters of Two C++ Strings
When only the first n characters matter, compare() can select a range from each string. The following example compares the first three characters of two strings.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "program";
string str2 = "project";
if (str1.compare(0, 3, str2, 0, 3) == 0) {
cout << "The first three characters are equal.";
} else {
cout << "The first three characters are not equal.";
}
return 0;
}
Output
The first three characters are equal.
Compare C-Style Strings with strcmp() in C++
std::string and C-style strings are different types. If you have null-terminated character arrays or const char* values, the C library function strcmp() from <cstring> can compare their contents.
Like std::string::compare(), strcmp() returns 0 when the two C-style strings are equal.
#include <cstring>
#include <iostream>
using namespace std;
int main() {
const char* str1 = "hello";
const char* str2 = "hello";
if (strcmp(str1, str2) == 0) {
cout << "The two C-style strings are equal.";
}
return 0;
}
Output
The two C-style strings are equal.
Do not compare two C-style string pointers with == when your intention is to compare the text they contain. Pointer equality asks whether the pointers refer to the same address, not whether two separate character sequences contain identical text.
Common C++ String Comparison Mistakes
- Checking
compare()as a Boolean: equality is represented by0, so usestr1.compare(str2) == 0. - Assuming comparisons ignore case:
"Hello"and"hello"are different strings under ordinarystd::stringcomparison. - Using
strcmp()forstd::stringunnecessarily: use==orcompare()directly forstd::stringobjects. - Using pointer equality for C strings: use
strcmp()when comparing the contents of null-terminated character sequences. - Assuming a specific negative or positive result from
compare(): test only whether it is less than, equal to, or greater than zero.
C++ String Equals Summary
For two std::string objects, use == when you only need to know whether their complete values are equal. Use std::string::compare() when you also need ordering information or want to compare selected ranges. Both standard string comparisons are case-sensitive. For null-terminated C-style strings, use content-aware functions such as strcmp() rather than comparing pointers with ==.
In this C++ Tutorial, we learned how to compare strings for equality using the equal-to comparison operator and the std::string::compare() function.
C++ String Equality Editorial QA Checklist
- Verify that examples using
std::stringdistinguish direct equality with==from the integer result returned bycompare(). - Confirm that equality with
compare()is always tested against0. - Check that case-sensitive behavior is stated clearly and that the case-insensitive example does not imply full Unicode case folding.
- Confirm that partial-string examples use valid
compare()ranges and explain what portion of each string is compared. - Verify that C-style string guidance uses
strcmp()for content comparison and does not recommend pointer equality. - Keep the distinction between
std::stringand null-terminated C-style strings explicit throughout the tutorial.
TutorialKart.com