Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use indexOf() case insensitively?

Tags:

c#

asp.net

I have list of strings:

List<string> fnColArr = new List<string>();
fnColArr={"Punctuation,period,Space,and,yes"};

I am using the IndexOf property for List to find the string in the current list:

int arrayval = fnColArr.IndexOf("punctuation");

Now the value of arrayval is -1, because the string is not present in the list. But here the only difference is the lower case.

I want to also find the string punctuation, regardless of its case.

like image 962
Bala Avatar asked Nov 24 '14 06:11

Bala


2 Answers

You can Use Overloaded Method

IndexOf("punctuation", StringComparison.OrdinalIgnoreCase);

Eg.

List<string> fnColArr = new List<string>() 
{ "Punctuation", "period", "Space", "and", "yes" };

            foreach (string item in fnColArr)
            {
                if (item.IndexOf("puNctuation", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    Console.WriteLine("match");

                }
            }
like image 128
Ganesh Avatar answered Oct 31 '22 20:10

Ganesh


Alternatively, you could also use a lambda function fnColArr.FindIndex(x => x.ToLower().Equals("punctuation"));

like image 31
Abbath Avatar answered Oct 31 '22 22:10

Abbath