.compare()
In C++, the .compare()
method compares two strings lexicographically (dictionary order) and returns an integer that indicates their relative order. The comparison is done character by character based on ASCII values.
Syntax
string1.compare(string2); // Compares string1 with string2.
string1.compare(pos, len, string2); // Compares a substring of string1 starting at pos with length len to the entirety of string2.
string1.compare(pos, len, string2, subpos, sublen); // Compares a substring of string1 (starting at pos, length len) with a substring of string2 (starting at subpos, length sublen)
Parameters:
string1
: The string object calling the.compare()
method.string2
: The string to compare with.pos
(optional: Starting position instring1
for comparisonlen
(optional): The number of characters to compare fromstring1
starting atpos
.subpos
(optional): The starting position instring2
from which to begin comparison.sublen
(optional): The number of characters to compare fromstring2
starting atsubpos
.
Return values:
- Returns
0
if the strings are equal - Returns a negative value (< 0) if
string1
is lexicographically less thanstring2
- Returns a positive value (> 0) if
string1
is lexicographically greater thanstring2
Example
This example demonstrates the usage of the .compare()
method:
#include <iostream>#include <string>int main() {std::string str1 = "apple";std::string str2 = "banana";std::string str3 = "apple";int result1 = str1.compare(str2);int result2 = str1.compare(str3);int result3 = str2.compare(str1);std::cout << "apple vs banana: " << result1 << std::endl;std::cout << "apple vs apple: " << result2 << std::endl;std::cout << "banana vs apple: " << result3 << std::endl;return 0;}
The output of this code is:
apple vs banana: -1apple vs apple: 0banana vs apple: 1
Codebyte Example
Run the following codebyte to understand how the .compare()
method works:
Frequently Asked Questions
1. What is the difference between .compare()
and ==
operator?
The ==
operator returns a boolean (true
or false
) indicating whether two strings are equal. The .compare()
method returns an integer indicating the relative order of the strings: less than, equal to, or greater than.
2. Can .compare()
be used for case-insensitive comparison?
No, .compare()
is case-sensitive by default. To perform a case-insensitive comparison, try converting both strings to the same case using methods like std::transform()
with ::tolower
, or use a custom comparator.
3. What happens when comparing strings of different lengths?
If one string is a prefix of the other, the shorter string is considered lexicographically smaller:
std::string a = "app";std::string b = "apple";bool result = a.compare(b) < 0; // true
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours