find and solve || findandsolve.com
Please wait.....

How to compare two strings in C#

In this Blog, You will learn how to compare two strings in C#. You will write one program to take two strings as input by the user. It will then compare the two strings and print out the result int. Will learn how to compare two strings, how to read user inputs, and how to use if-else conditions in the C# programming language.

The simplest form of comparing two strings for the same value is a string. Equals method. If both strings are equal, the method returns true else false. Compares substrings of two specified String objects using the specified rules, and returns an integer that indicates their relative position in the sort order.

You will use the below algorithm :

  • Enter first string value from user input. Read and store it in a variable.
  • Enter second string value from user input. Read and store it in a different variable.
  • Compare both strings.
  • Print the message to the user accordingly.

Create a C# Console Application

  • Open your visual studio 2019 or greater than 2019
  • Click on Create a New Project
  • Select Console Application and then Next
  • Provide a Project name and confirm or change the Location. Click Next


  • Select the Target Framework under drop-down (.NET Core 5.0) and Click on Create button.


Using String.Compare

String.Compare method compares two strings and returns an integer value. The return value of the Compare method can be less than zero, greater than zero, or equal to zero.

  • If the result is less than 0, the first string precedes the second string in the sorting order.
  • If the result is 0, both strings will be in the same position of sort order or both strings are equal.
  • If the result is greater than 0, the first string follows the second string in the sort order.


string.Compare Example in C# program 

using System;
namespace FindAndSolve
{
   public class Program
    {
        public static void Main(string[] args)
        {         
            string firstStr, secondStr;         
            Console.WriteLine("Enter the first string : ");
            firstStr = Console.ReadLine();
           
            Console.WriteLine("Enter the second string : ");
            secondStr = Console.ReadLine();
                        if (string.Compare(firstStr,secondStr) == 0)
            {
                Console.WriteLine("Both strings are equal");
            }
            else if(string.Compare(firstStr, secondStr) < 0)
            {   
                Console.WriteLine("The first string is smaller than the second string");
            }
            else
            {
                Console.WriteLine("The first string is greater than the second string");
            }
        }
    }
}

Output

Enter the first string : 
findandsolve
Enter the second string :
findandsolve
Both strings are
equal

Enter the first string : 
find
Enter the second string :
findandsolve
The first string is smaller than the second st
ring

Enter the first string :
findandsolve
Enter the second string :
findand
The first string is greater than the second stri
ng