In this examples, we’ll learn how to compare two strings in C# Console Application using case sensitive and case insensitive.
Example 1: Compare Two Strings with case sensivity.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
static void Main(string[] args) { string str1 = "Csharp"; string str2 = "csharp"; if(str1==str2) { Console.WriteLine("Equal"); } else { Console.WriteLine("Not Equal"); } Console.ReadKey(); } |
Example 2: Compare Two Strings using String.Equals with case
sensitivity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
static void Main(string[] args) { string str1 = "Csharp"; string str2 = "csharp"; if(String.Equals(str1,str2)) { Console.WriteLine("Equal"); } else { Console.WriteLine("Not Equal"); } Console.ReadKey(); } |
When you run the program, the output will be:
Example 3: Compare two strings without case sensitivity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
static void Main(string[] args) { string str1 = "Csharp"; string str2 = "csharp"; if(str1.ToLower()==str2.ToLower()) { Console.WriteLine("Equal"); } else { Console.WriteLine("Not Equal"); } Console.ReadKey(); } |
Example 4: Compare two strings without case sensitivity using
String.Equals.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
static void Main(string[] args) { string str1 = "Csharp"; string str2 = "csharp"; if(String.Equals(str1,str2, StringComparison.CurrentCultureIgnoreCase)) { Console.WriteLine("Equal"); } else { Console.WriteLine("Not Equal"); } Console.ReadKey(); } |
When you run the program, the output will be: