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

One Response to Case-insensitive string compare in .NET

  1. Marc Delahousse says:

    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

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>