— New online course!  Getting Started with Azure DevOps — Special launch price 50% off —

Case-insensitive string compare in .NET

by

I’ve been writing code full-time in C# for about 4 years now but every once in a while someone shows me something totally new.  Last week, one of the devs on my team showed me an override of String.Equals() that does a case-insensitive compare.  It kinda blew my mind that I’d never seen that before.


The following line of code evaluates to true.



string.Equals(“string number 1”, “String NUMBER 1”, StringComparison.CurrentCultureIgnoreCase) 


There is also a similar override of String.Compare().


Pretty handy, huh? 


-Ben


2 Responses to "Case-insensitive string compare in .NET"
  1. I am working in C++ but this applies to the topic.

    I’ve often com across bugs where developers forgot to take case insensitivity into comparison. As a result, these bugs show up again and again. So Ièm currently working on a CaseeInsensitiveString, this avoids the proplem altogether by forcing the USE of the string to be case insensitive. This is great for things like IDs, paths, and people’s names.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: