Saturday 6 August 2016

C# - String Comparison ignore case the best way

As a C# developer, we tend to compare strings more frequently and we always tend to make lot of UpperCase and LowerCase conversions before comparing strings. 

To simply comparision try using "StringComparision.OrdinalIgnore", below is the sample

Code:
String string1 = "Test", string2 = "test";
Console.WriteLine("Strings are similar : {0}",string1.Equals(string2, StringComparison.OrdinalIgnoreCase).ToString());

Output:
Strings are similar : True


Refer below for more options:
https://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx


     

No comments:

Post a Comment