Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine List.IndexOf ignoring case [duplicate]

Tags:

c#

list

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
like image 387
c0rd Avatar asked Sep 12 '16 07:09

c0rd


People also ask

Does indexOf ignore case?

The indexOf() method returns the index number where the target string is first found or -1 if the target is not found. Like equals(), the indexOf() method is case-sensitive, so uppercase and lowercase chars are considered to be different.

Is indexOf function case sensitive?

The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.

Is indexOf case sensitive C#?

IndexOf Method is by default case sensitive. But we can make it case insensitive if we use one of the overloads: IndexOf(String, StringComparison).


1 Answers

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));
like image 143
Jaydip Jadhav Avatar answered Oct 22 '22 01:10

Jaydip Jadhav