如何不区分大小写地使用indexOf()?

Bal*_*ala 1 c# asp.net

我有字符串列表:

List<string> fnColArr = new List<string>();
fnColArr={"Punctuation,period,Space,and,yes"};
Run Code Online (Sandbox Code Playgroud)

我正在使用该IndexOf属性List在当前列表中查找字符串:

int arrayval = fnColArr.IndexOf("punctuation");
Run Code Online (Sandbox Code Playgroud)

现在的值arrayval是-1,因为该字符串不在列表中。但是这里唯一的区别是小写字母。

我还想找到字符串punctuation,无论大小写如何。

Gan*_*kar 5

您可以使用重载方法

IndexOf("punctuation", StringComparison.OrdinalIgnoreCase);
Run Code Online (Sandbox Code Playgroud)

例如。

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");

                }
            }
Run Code Online (Sandbox Code Playgroud)