Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Case insensitive string comparison [duplicate]

Tags:

c#

list

I have a list that holds a few strings(names). For this example. It will hold:

  • TeSt1
  • TeSt2
  • TeSt3

And I'm trying to check if that list has one of those. And I'm doing this like this at the moment:

if (list.Contains(test2))
{

}

But I need it to be case insensitive.. But how can I do that? in an if statement.

like image 837
Stian Tofte Avatar asked Oct 23 '25 02:10

Stian Tofte


1 Answers

The Contains method has an overload that accepts an IEqualityComparer. You can give it one by doing the following:

 if (list.Contains(test2, StringComparer.OrdinalIgnoreCase))  
 {  
     // do something  
 }
like image 86
carpenter Avatar answered Oct 25 '25 15:10

carpenter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!