16

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,329
  • 1
  • 13
  • 20

2 Answers2

28

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
  • 12,179
  • 6
  • 24
  • 40
-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
  • 27,717
  • 28
  • 128
  • 190
Eskir
  • 644
  • 1
  • 7
  • 12