快速多字符串比较 [英] Fast multiple string compare
本文介绍了快速多字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在c#中,有一种快速方法可以用更有效的代码替换以下内容:
In c# is there a quick way to replace the following with more efficient code:
string letters = "a,b,c,d,e,f";
if (letters.Contains("a"))
{
return true;
}
if (letters.Contains("b"))
{
return true;
}
if (letters.Contains("c"))
{
return true;
}
我想消除必须包含三行比较的代码.
I want to do away with having to have three compare lines of code.
谢谢!
推荐答案
您可以使用以下内容:
return letters.Any(c => c == 'a' || c == 'b' || c == 'c');
或者这个:
var lettersToLookFor = "abc";
return letters.Any(c => lettersToLookFor.Contains(c));
根据注释,可以进一步缩短前一个代码块的最后一行:
As per the comments, the last line of the previous code block can be shortened further:
return letters.Any(lettersToLookFor.Contains);
这篇关于快速多字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文