忽略Linq.Any,C#中的大小写 [英] Ignoring case in Linq.Any, C#

查看:63
本文介绍了忽略Linq.Any,C#中的大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有忽略用户输入中常用词的代码:

I have code that ignores common words from user input:

string[] ignored_words = { "the", "and", "I" };

String[] words = UserInput.Split(' ');
foreach (string word in words)
{
    if (!ignored_words.Any(word.Equals))
    {
        // perform actions on desired words only
    }
}

这很好用,除非大小写错误("THE",因为用户输入不会被"the"被忽略的单词捕获).

This works great, unless the case is wrong ("THE" as user input will not be caught by the "the" ignored word).

如何将 IgnoreCase子句添加到等于比较?

How can I add an IgnoreCase clause to the Equals comparison?

推荐答案

if (!ignored_words.Any(w => w.Equals(word, StringComparison.CurrentCultureIgnoreCase)))
{
   // ...
}

或使用静态 String.等于 ,其中 null 值没有问题:

or with the static String.Equals which has no issues with null values:

if (!ignored_words.Any(w => string.Equals(w, word, StringComparison.CurrentCultureIgnoreCase)))
{
   // ...
}

这篇关于忽略Linq.Any,C#中的大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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