includes() method case insensitive, convert both of the strings in the comparison to lowercase, e.g. str. toLowerCase(). includes(substr. toLowerCase()) .
Both String#includes() and String#indexOf() are case sensitive. Neither function supports regular expressions. To do case insensitive search, you can use regular expressions and the String#match() function, or you can convert both the string and substring to lower case using the String#toLowerCase() function.
array.Contains("str", StringComparer.OrdinalIgnoreCase);
Or depending on the specific circumstance, you might prefer:
array.Contains("str", StringComparer.CurrentCultureIgnoreCase);
array.Contains("str", StringComparer.InvariantCultureIgnoreCase);
Some important notes from my side, or at least putting some distributed info at one place- concerning the tip above with a StringComparer like in:
if (array.Contains("str", StringComparer.OrdinalIgnoreCase))
{}
array.Contains()
is a LINQ extension method and therefore works by standard only with .NET 3.5 or higher, needing:using System;
using System.Linq;
But: in .NET 2.0 the simple Contains()
method (without taking case insensitivity into account) is at least possible like this, with a cast:
if ( ((IList<string>)mydotNet2Array).Contains(“str”) )
{}
As the Contains() method is part of the IList interface, this works not only with arrays, but also with lists, etc.
Implement a custom IEqualityComparer that takes case-insensitivity into account.
Additionally, check this out. So then (in theory) all you'd have to do is:
myArray.Contains("abc", ProjectionEqualityComparer<string>.Create(a => a.ToLower()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With