C# program to check given strings are equal or not using equal to (==) operator

C# code for string comparison:

// C# program to check given strings are equal or not 
// using equal to (==) operator
using System;
using System.IO;
using System.Text;

namespace JustTechReview
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            string str1;
            string str2;

            //input strings
            Console.Write("Enter a string: ");
            str1 = Console.ReadLine();
            Console.Write("Enter another string: ");
            str2 = Console.ReadLine();

            //comparing strings 
            if (str1 == str2)
                Console.WriteLine("\"{0}\" and \"{1}\" are equal", str1, str2);
            else
                Console.WriteLine("\"{0}\" and \"{1}\" are not equal", str1, str2);

            //another way 
            if ((str1 == str2) == true)
                Console.WriteLine("\"{0}\" and \"{1}\" are equal", str1, str2);
            else
                Console.WriteLine("\"{0}\" and \"{1}\" are not equal", str1, str2);

            //comparing by ignoring the case 
            //convert both strings in the same case 
            //either in uppercase or lowercase
            Console.WriteLine("By ignoring case...");
            if(str1.ToUpper() == str2.ToUpper())
                Console.WriteLine("\"{0}\" and \"{1}\" are equal", str1, str2);
            else
                Console.WriteLine("\"{0}\" and \"{1}\" are not equal", str1, str2);

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output:

First run:
Enter a string: JustTechReview
Enter another string: JustTechReview
"JustTechReview" and "JustTechReview" are equal
"JustTechReview" and "JustTechReview" are equal
By ignoring case...
"JustTechReview" and "JustTechReview" are equal

Second run:
Enter a string: justtechreview
Enter another string: JUSTTECHREVIEW
"justtechreview" and "JUSTTECHREVIEW" are not equal
"justtechreview" and "JUSTTECHREVIEW" are not equal
By ignoring case...
"justtechreview" and "JUSTTECHREVIEW" are equal

Third run:
Enter a string: justtechreview
Enter another string: JustTechReview.com
"JustTechReview" and "JustTechReview.com" are not equal
"JustTechReview" and "JustTechReview.com" are not equal
By ignoring case...
"JustTechReview" and "JustTechReview.com" are not equal

Leave a Comment

error: Alert: Content is protected!!