c# - 如何忽略 List<string> 中的区分大小写

标签 c#

假设我有这段代码

string seachKeyword = "";
List<string> sl = new List<string>();
sl.Add("store");
sl.Add("State");
sl.Add("STAMP");
sl.Add("Crawl");
sl.Add("Crow");
List<string> searchResults = sl.FindAll(s => s.Contains(seachKeyword));

如何在包含搜索中忽略字母大小写?

谢谢,

最佳答案

使用 Linq,这为 .Compare 添加了一个新方法

using System.Linq;
using System.Collections.Generic;

List<string> MyList = new List<string>();
MyList.Add(...)
if (MyList.Contains(TestString, StringComparer.CurrentCultureIgnoreCase)) {
    //found
} 

大概是这样

using System.Linq;
...

List<string> searchResults = sl.FindAll(s => s.Contains(seachKeyword, StringComparer.CurrentCultureIgnoreCase));  

关于c# - 如何忽略 List<string> 中的区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3107765/

相关文章:

c# - 如何对空枚举进行单元测试?

c# - "net.pipe://"不支持请求的升级

c# - Mono编译时如何引用Microsoft.Boogie?

c# - 如何使用具有相同契约(Contract)和绑定(bind)的动态端点创建多个 wcf 服务实例而不保存在 app.config 中?

c# - 覆盖 ASP.NET MVC 站点中的 View 不起作用

c# - 如何在 T4 模板中使用 C# v6 的最新功能?

c# - NDesk.Options/Mono.Options : Parameter with multiple key/value pairs

c# - ASP.NET 路由 - 如何响应 .aspx 请求的 404

c# - 如果有任何格式,我该如何格式化以仅包括小数

c# - 为什么我们使用 HttpContext.Current?