Define List.IndexOf ignoring case

Is there a way to get the index of an item in a case-sensitive list?

List<string> sl = new List<string>() { "a","b","c"};
int result = sl.IndexOf("B"); // should be 1 instead of -1
+4
source share
3 answers

Try the following: . Therefore, there is no direct way to use IndexOf with the String Comparison option for LIST, in order to achieve the result of the desire, you need to use the Lambda expression.

int result = sl.FindIndex(x => x.Equals("B",StringComparison.OrdinalIgnoreCase));
+5
source

You can use a method IndexOf()that takes a type StringComparison:

  string s = "foobarbaz";
    int index = s.IndexOf("BAR", StringComparison.CurrentCultureIgnoreCase); // index = 3

If the string is not found, IndexOf()returns -1.

Find a case insensitive substring - C #

+1
source

IndexOf # ComparisonType, :

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

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

-3

Source: https://habr.com/ru/post/1654317/


All Articles