How can I use the compare() method of std::string in a C++ app? How can we use C++ to compare strings? Here are the answers with C++ examples.
In addition to int, float, double there is another data type called string that we use for alphanumeric variables. In C++ there are several typedefs for common character types are provided: String types are defined in header <string>.
strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In another terms string stores for the alphanumeric text with 1 byte chars, called ASCII chars. Strings are the instantiation of the basic_string class template that uses char as the character type. Simply we can define a string as below,
1 2 3 |
std::string str = "This is a String"; |
String
has methods to assign, copy, align, replace or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. Let’s see comparison methods now.
How to compare two strings with the ‘==’, ‘>’ and ‘<‘ Operators in a C++ app
Simply we can use ‘==’, ‘>’ and ‘<‘ operators to compare strings as we do in numbers. Let’s assume that we have two strings s1 and s2. If we use these operators, we have 3 possibilities.
if they are equal to : that means they are same, both character sequences compare equivalent
if s1<s2 : character sequence in lexicographical order, string s2 is greater than the string s1
if s2>s1 : character sequence in lexicographical order, string s1 is greater than the string s2
Here is a example to compare s1 and s2 strings in C++;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> int main() { std::string s1, s2; s1 = "test"; s2 = "test"; if( s1 == s2 ) std::cout << "same strings" << std::endl; if( s1 < s2 ) std::cout << "s1 < s2" << std::endl; if( s1 > s2 ) std::cout << "s1 > s2" << std::endl; getchar(); return 0; } |
and the output will be,
1 2 3 |
same strings |
How to compare two strings in a C++ app using the compare() method
We can compare a string with another string by using compare() method. Let’s see this method now.
compare() method of std::string, compares two strings in accordance with their character sequences.
1 2 3 |
int compare( const basic_string& str ) const noexcept; // Until C++20 |
return value of compare() method is used to check comparison:
compare(s)==0 : both character sequences compare equivalent
compare(s)<0 : appears before the character sequence specified by the arguments, in lexicographical order
compare(s)>0 : appears after the character sequence specified by the arguments, in lexicographical order
- compare() returns int, while relational operators return boolean value i.e. either true or false.
- A single Relational operator is unique to a certain operation, while compare() can perform lots of different operations alone, based on the type of arguments passed.
- We can compare any substring at any position in a given string using compare(), which otherwise requires the long procedure of word by word extraction of string for comparison using relational operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> int main() { std::string s1, s2; s1 = "test"; if(s1.compare("test")==0) std::cout << "same strings"; getchar(); return 0; } |
and the output will be,
1 2 3 |
same strings |
A substring comparison with the C++ compare() Method
As same above we can compare part of a string with another string. here is the syntax below,
1 2 3 |
int compare( size_type start_position, size_type char_count, const basic_string& str ) const; // Since C++11, Until C++20 |
Here is an example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> int main() { std::string s1, s2; s1 = "C++ Programming"; s2 = "Program"; // Let's compare s2 in exact position from 4 to 4 + its length if( s1.compare( 4, s3.length(), s3 ) == 0 ) std::cout << "Compared Substring and both same wide strings" << std::endl; getchar(); return 0; } |
This compare() method can be used with std::wstrings too.