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

How To Make A Comparison Between Strings In A C++ App

How To Make A Comparison Between Strings In A C++ App

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,

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

and the output will be,

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.

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,

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,

Here is an example,

This compare() method can be used with std::wstrings too.

close

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.

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 was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap