In this example, you’ll learn How to compare two strings in C# using if condition.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
static void Main(string[] args) { string s1, s2; Console.Write("String 1: "); s1 = Console.ReadLine(); Console.Write("String 2: "); s2 = Console.ReadLine(); if (s1 == s2) { Console.WriteLine("Equal"); } else { Console.WriteLine("Not Equal"); } Console.ReadKey(); } |
When you run the program, the output will be:
In the above program, we’ve two strings s1 and s2. We simply use equality operator (==
) to compare the two strings, which compares the value Csharp to Python and prints Not Equal.