-
-
About once every couple of months I need to write a bit of code that does one thing if a particular string is found within an array of strings and something else if it is not ignoring differences in case. For whatever reason, I never seem to remember the code snippet to accomplish this, so after spending 10 minutes of research today I thought I'd write it down here in an effort to help commit it to memory or, at the very least, serve as a quick place to find the answer when the need arises again.
So without further adieu, here it is:
Visual Basic Version:
If stringArrayName.Contains("valueToLookFor", StringComparer.OrdinalIgnoreCase) Then
...
Else
...
End If
C# Version:
if (stringArrayName.Contains("valueToLookFor", StringComparer.OrdinalIgnoreCase))
...
else
...
Without the StringComparer.OrdinalIgnoreCase the search will be case-sensitive. For more information on comparing strings, see: New Recommendations for Using Strings in Microsoft .NET 2.0.
Happy Programming!