如何忽略列表&LT区分大小写;字符串> [英] How to ignore the case sensitivity in List<string>

查看:129
本文介绍了如何忽略列表&LT区分大小写;字符串>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有这样的代码

Let us say I have this code

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



我怎么能忽略大小写的包含搜索?

How can I ignore the letter case in Contains search?

谢谢,

推荐答案

最好的办法是使用顺序不区分大小写的比较,但是包含方法不支持它

The best option would be using the ordinal case-insensitive comparison, however the Contains method does not support it.

您可以使用以下方法来做到这一点:

You can use the following to do this:

sl.FindAll(s => s.IndexOf(searchKeyword, StringComparison.OrdinalIgnoreCase) >= 0);



这将是更好的扩展方法来包装这一点,如:

It would be better to wrap this in an extension method, such as:

public static bool Contains(this string target, string value, StringComparison comparison)
{
    return target.IndexOf(value, comparison) >= 0;
}



所以,你可以使用:

So you could use:

sl.FindAll(s => s.Contains(searchKeyword, StringComparison.OrdinalIgnoreCase));

这篇关于如何忽略列表&LT区分大小写;字符串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆