14

Is there a way to get the index of a item within a List with case insensitive search?

List<string> sl = new List<string>() { "a","b","c"};
int result = sl.IndexOf("B"); // should be 1 instead of -1
c0rd
  • 1,149
  • 1
  • 11
  • 19

2 Answers2

24

Try this : So there is no direct way to use IndexOf with String Comparison option for LIST, to achieve desire result you need to use Lambda expression.

int result = sl.FindIndex(x => x.Equals("B",StringComparison.OrdinalIgnoreCase));
Jaydip Jadhav
  • 11,858
  • 6
  • 23
  • 39
-6

The IndexOf method for Strings in C# has a ComparisonType argument, which should work something like this:

sl.IndexOf("yourValue", StringComparison.CurrentCultureIgnoreCase)

or

sl.IndexOf("yourValue", StringComparison.OrdinalIgnoreCase)

Documentation for this can be found here and here

Liam
  • 25,247
  • 27
  • 110
  • 174
Eskir
  • 514
  • 5
  • 12