17

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
0

2 Answers 2

29

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));
Sign up to request clarification or add additional context in comments.

Comments

-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

3 Comments

As @c0rd said in response to another answer for this thread, this isn't about a string, it's about a List<string>.
For what it’s worth, I downvoted that answer too. I hope there are no hard feelings. Just trying to help.
This answer was useful for me..I upvoted it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.