X

C# tip How to String compare

dotnet C#

Here is a quick C# tip, when using string compare don’t use

var str = "Hello";

if(str.ToLower() === "hello")

use the Equals and StringComparison.InvariantCultureIgnoreCase enum.


var str = "Hello";

if(str.Equals("hello", StringComparison.InvariantCultureIgnoreCase))

The thing you need to note is which rules are appropriate current culture, the invariant culture, ordinal or another culture entirely.
One can create the compare from StringComparer.Create(culture, true)

Categories: .NET C#
Taswar Bhatti:
Related Post