C++C++11C++14C++17Introduction to C++Learn C++

How To Make Wide String Comparisons In Modern C++ Software

How To Make Wide String Comparisons In Modern C++ Software

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.

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,

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++;

and the output will be,

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.

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

  1. compare() returns int, while relational operators return boolean value i.e. either true or false. 
  2. 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.
  3. 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. 

and the output will be,

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,

Here is an example,

and the output will be,

Note that in addition to std::wstrings, this compare() method can be used with std::strings too.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?