Azure DevOps, Scrum, & .NET Software Leadership and Consulting Services

Introducing Slide Speaker: Videos with Voice-over from your PowerPoint and Google Slides Presentations

Case-insensitive string compare in .NET


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

SUBSCRIBE TO THE BLOG

,

2 responses to “Case-insensitive string compare in .NET”

  1. Marc Delahousse Avatar
    Marc Delahousse

    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.

  2. Pavel Vladov Avatar

    If you want your code to be even faster, use the OrdinalIgnoreCase string comparison type. For more information about the case insensitive string comparison routines in C# take a look at:

    C# Case Insensitive String comparison

Leave a Reply

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

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