Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a case-insensitive option to Array.IndexOf

Tags:

c#

asp.net

People also ask

How do you make indexOf case insensitive?

indexOf(someStr. toLowerCase()); That will do a case insensitive indexOf() .

Is array indexOf case sensitive?

We can't get the index of an element by performing a case insensitive lookup with Array. indexOf , because the method takes in the value directly and does not allow us to iterate over each array element and manipulate them (e.g. lowercase).

Can you do indexOf for an array?

Introduction to the JavaScript array indexOf() methodTo find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.


Beaware !! The Answer marked might have some problem , like

string array[] = {"hello", "hi", "bye" , "welcome" , "hell"}

if you use the same method as described in the answer to find the index of word "hell"

Int Indexofary = Array.FindIndex(array, t => t.IndexOf("hell", StringComparison.InvariantCultureIgnoreCase) >=0);

you will get result Indexofary = 0 instead of 4.

Instead of that use

Array.FindIndex(array, t => t.Equals("hell", StringComparison.InvariantCultureIgnoreCase));

to get proper result .

Rrgards Bits


Since you are looking for index. Try this way.

Array.FindIndex(myarr, t => t.IndexOf(str, StringComparison.InvariantCultureIgnoreCase) >=0);

Array.IndexOf calls the default "Equals" method which is case-sensitive. Try this:

Array.FindIndex(myarr, t => t.Equals(str, StringComparison.InvariantCultureIgnoreCase))

var result = myarr.FindIndex(s => s.Equals(str, StringComparison.OrdinalIgnoreCase));