The C++ std::string::compare() function is used to compare two strings. It provides a way to lexicographically compare the content of the string objects with another string or a sub string. It returns a integer that are listed below :
0: The strings are equal
<0: The calling string is less than the argument string.
> 0: The calling string is greater than the argument string.
SyntaxThis function can be used to compare the entire string or a specified range of characters.
Following is the syntax for std::string::compare() function.
int compare (const string& str) const noexcept; or int compare (size_t pos, size_t len, const string& str) const; int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const; or int compare (const char* s) const; int compare (size_t pos, size_t len, const char* s) const; or int compare (size_t pos, size_t len, const char* s, size_t n) const;Parameters
It returns a signed integral indicating the relation between the strings.
Exceptionsif an exception is thrown, there are no changes in the string.
Example 1Following is the basic example to demonstrate the string::compare using C++.
#include <iostream> #include <string> using namespace std; int main() { string X1 = "apple"; string X2 = "banana"; int result = X1.compare(X2); if (result == 0) { cout << " Both are equal " << endl; } else if (result < 0) { cout << X1 << " is less than " << X2 << endl; } else if (result > 0) { cout << X1 << " is greater than " << X2 << endl; } return 0; }Output
Let us compile and run the above program, this will produce the following result −
apple is less than bananaExample 2
In this example, we are comparing one substring with the another string. So it prints the output as equal.
#include <iostream> #include <string> using namespace std; int main() { string X1 = "Tutorialspoint"; string X2 = "point"; int result = X1.compare(9, 5, X2); if (result == 0) { cout << " Both are equal " << endl; } else if (result < 0) { cout << X1.substr(6, 5) << " is less than " << X2 << endl; } else if (result > 0) { cout << X1.substr(6, 5) << " is greater than " << X2 << endl; } return 0; }Output
If we run the above code it will generate the following output.
Both string are equal.Example 3
Following is an another example to compare substrings of two strings using string::compare() function.
#include <iostream> #include <string> using namespace std; int main() { string X1 = "hello world"; string X2 = "goodbye world"; int result = X1.compare(6, 5, X2, 8, 5); if (result == 0) { cout << " Both are equal " << endl; } else if (result < 0) { cout << X1.substr(6, 5) << " is less than " << X2.substr(8, 5) << endl; } else { cout << X1.substr(6, 5) << " is greater than " << X2.substr(8, 5) << endl; } return 0; }Output
Following is the output of the above code.
The substrings are equal.Example 4
In this example, we compare with one string with the empty string.
#include <iostream> #include <string> using namespace std; int main() { string X1 = "hello"; string X2 = ""; int result = X1.compare(X2); if (result == 0) { cout << " Both are equal " << endl; } else if (result < 0) { cout << X1 << " is lesser than " << X2 << endl; } else { cout << X1 << " is greater than " << X2 << endl; } return 0; }Output
Following is the output of the above code.
hello is greater than
string.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4