How can I use compare() method of std::wstring in my C++ software? How can we compare wide strings? Here are the answers with C++ examples.
Table of Contents
What is a C++ Wide String?
Wide string types are defined in header <string>. wstrings (Wide Strings) is the string class for characters that has 2 bytes represented with wstring. In Modern C++ software alphanumeric characters are stored in wide strings because of their supports to characters of world languages and displayed in wstring forms. In other terms wstring stores for the alphanumeric text with 2-byte chars, called wchar_t or WChar. Wide strings are the instantiation of the basic_string class template that uses wchar_t as the character type. Simply we can define a wstring as below,
1 2 3 |
std::wstring str = L"This is a Wide String"; |
as you see we use L” ” literal to define wide strings in Unicode form.
wstring has methods to assign, copy, align, replace, compare or to operate with other wide strings. These methods can be used in all wide string methods with their appropriate syntax. Let’s see comparison methods.
Wide string comparison with ‘==’, ‘>’ and ‘<‘ operators in your C++ software
Simply we can use ‘==’, ‘>’ and ‘<‘ operators to compare wide strings as we do in numbers. Let’s assume that we have two wide 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, wide string s2 is greater than the wide string s1
if s2>s1 : character sequence in lexicographical order, wide string s1 is greater than the wide string s2
Here is an 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 22 |
#include <iostream> #include <string> int main() { std::wstring s1, s2; s1 = L"C++ Programming"; s2 = L"C++ Programming"; if( s1 == s2 ) std::cout << "both wide strings are same" << 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 |
both wide strings are same |
C++ Wide String comparison with the compare() Method
We can compare a wide string with another wide 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 |
#include <iostream> #include <string> int main() { std::wstring s1, s2; s1 = L"C++ Programming"; if( s1.compare(L"C++ Programming") == 0 ) std::cout << "same wide strings"; getchar(); return 0; } |
and the output will be,
1 2 3 |
same wide strings |
Substring of a Wide String Comparison with the compare() C++ software method
As same above we can compare part of a wide string with another wide 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 21 |
#include <iostream> #include <string> int main() { std::wstring s1, s2; s1 = L"C++ Programming"; s2 = L"Program"; // Let's compare s2 in exact position from 4 to 4 + its length if( s1.compare( 4, s2.length(), s2 ) == 0 ) std::cout << "Compared Substring and both same wide strings" << std::endl; getchar(); return 0; } |
and the output will be,
1 2 3 |
Compared Substring and both same wide strings |
Note that in addition to std::wstrings, this compare() method can be used with std::strings too.