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

标签 c# asp.net

我有字符串列表:

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

我正在使用 ListIndexOf 属性来查找当前列表中的字符串:

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/

相关文章:

c# - 我如何找出哪个进程正在使用 .NET 锁定文件?

c# - 重写列表框控件以返回连接的字符串值

asp.net - 找不到适合实体类型 'Uri' 的构造函数。

c# - 集中 C# 和 sql 使用的业务逻辑

c# - 编写一个文件,在每行的开头添加随机字符

asp.net - 将 DropDownList 绑定(bind)到没有数据源的详细信息 View

c# - 在 asp.net mvc c# 中将钱格式化为货币

html复选框输入大小错误

c# - 如何使用asp.net(C#)注册新用户?

c# - ApplicationInsights - 如何跟踪 WebAPI 使用情况