I've found answers to determining if an IList<string>
contains an element using case insensitive contains: ilist.Contains(element, StringComparer.CurrentCultureIgnoreCase)
But what I would like to do is find the element itself that corresponds within the IList to the element I'm searching for. For instance if the IList contains {Foo, Bar}
and I search for fOo
I'd like to be able to receive Foo
.
I'm not worried about multiples, and the IList doesn't seem to contain any function other than IndexOf
with doesn't help me much.
EDIT: As I'm using IList and not List, I don't have the function IndexOf, so the answer posted on here doesn't help me much :)
To find the index of the item you could use the FindIndex
function with a custom predicate doing the case-insensitve match. Similarly, you can use Find
to get the actual item.
I'd probably create an extension method to use as an overload.
public static int IndexOf(this List<string> list, string value, StringComparer comparer)
{
return list.FindIndex(i => comparer.Equals(i, value));
}
public static int CaseInsensitiveIndexOf(this List<string> list, string value)
{
return IndexOf(list, value, StringComparer.CurrentCultureIgnoreCase);
}
public static string CaseInsensitiveFind(this List<string> list, string value)
{
return list.Find(i => StringComparer.CurrentCultureIgnoreCase.Equals(i, value));
}