如何验证一个字符串是否存在于一个常量字符串列表中

huangapple go评论76阅读模式
英文:

How to verify the presence of a string in a constant list of strings

问题

以下是翻译好的代码部分:

// 第一个尝试
if (Destination.Name in new[] { "MA", "MB", "MC", "MD", "ME", "MF", "MG" })

// 经过一些搜索后的尝试
if (new[] { "MA", "MB", "MC", "MD", "ME", "MF", "MG" }.Contains(Destination.Name))

在C#中,这是完成你所需任务的两种最简单的方式之一。

英文:

This should be easy, but I don't find the correct syntax: I would like to know if my name is present in a contant list of strings.

These are my attempts:

// first idea
if (Destination.Name in {"MA", "MB", "MC", "MD", "ME", "MF", "MG"})

// after some searching
if IndexOf(new string[] { "MA", "MB", "MC", "MD", "ME", "MF", "MG" }, Destination.Name)

... but none of them seems to work.

What's the easiest way to get this done in C#?

Thanks in advance

答案1

得分: 4

如果我理解正确,您正在寻找Destination.Name"MA","MB","MC","MD","ME","MF","MG"中的情况(即如果Destination.NameMAMB等):

if (new string[] { "MA", "MB", "MC", "MD", "ME", "MF", "MG" }.Contains(Destination.Name)) 
{
  ...
}

对于IndexOf也是类似的,我们检查Destination.Name在数组中的索引是否不为负数:

// 要么使用List,要么比较繁琐
// if (Array.IndexOf(new string[] {...}, Destination.Name)) {...}
if (new List<string> { "MA", "MB", "MC", "MD", "ME", "MF", "MG" }
      .IndexOf(Destination.Name) >= 0) 
{
  ...
}

最后,在一般情况下,您可以使用Linq查询数组。虽然这里有点过头了,但如果您有非平凡的条件,Lambda函数(这里是item => item == Destination.Name)可能会非常灵活。

using System.Linq;

...

// 如果数组中有任何与Destination.Name相等的项
if (new string[] { "MA", "MB", "MC", "MD", "ME", "MF", "MG" }
      .Any(item => item == Destination.Name))
{
   ...
}
英文:

If I understand you right, you are looking for Destination.Name in &quot;MA&quot;, &quot;MB&quot;, &quot;MC&quot;, &quot;MD&quot;, &quot;ME&quot;, &quot;MF&quot;, &quot;MG&quot; (i.e. if Destination.Name is MA or MB etc.):

if (new string[] { &quot;MA&quot;, &quot;MB&quot;, &quot;MC&quot;, &quot;MD&quot;, &quot;ME&quot;, &quot;MF&quot;, &quot;MG&quot; }.Contains(Destination.Name)) 
{
  ...
}

Same for IndexOf: we check if Destination.Name index within the array is not negative:

// Either use List, or ugly
// if (Array.IndexOf(new string[] {...}, Destination.Name)) {...}
if (new List&lt;string&gt; { &quot;MA&quot;, &quot;MB&quot;, &quot;MC&quot;, &quot;MD&quot;, &quot;ME&quot;, &quot;MF&quot;, &quot;MG&quot; }
      .IndexOf(Destination.Name) &gt;= 0) 
{
  ...
}

Finally, in general case you can query array with a help of Linq.
It's an overshoot here, but if you have a non-trivial condition, lambda
function (here it is item =&gt; item == Destination.Name) could be very flexible.

using System.Linq;

...

// If the array has any item which is equal to Destination.Name 
if (new string[] { &quot;MA&quot;, &quot;MB&quot;, &quot;MC&quot;, &quot;MD&quot;, &quot;ME&quot;, &quot;MF&quot;, &quot;MG&quot; }
      .Any(item =&gt; item == Destination.Name))
{
   ...
}

huangapple
  • 本文由 发表于 2023年6月27日 19:59:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定