我有字符串列表:
List<string> fnColArr = new List<string>();
fnColArr={"Punctuation,period,Space,and,yes"};
我正在使用 List
的 IndexOf
属性来查找当前列表中的字符串:
int arrayval = fnColArr.IndexOf("punctuation");
现在 arrayval
的值为 -1,因为该字符串不在列表中。但这里唯一的区别是小写。
我还想找到字符串 punctuation
,不管它的大小写如何。
最佳答案
你可以使用重载方法
IndexOf("punctuation", StringComparison.OrdinalIgnoreCase);
例如。
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");
}
}
关于c# - 如何不区分大小写地使用 indexOf()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27099127/