字符串在数组中吗? [英] Is string in array?
本文介绍了字符串在数组中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
查看 string[]
是否包含元素的最佳方法是什么.这是我第一次尝试.但也许我忽略了一些东西.数组大小不超过 200 个元素.
What would be the best way to look in a string[]
to see if it contains a element. This was my first shot at it. But perhaps there is something that I am overlooking. The array size will be no larger than 200 elements.
bool isStringInArray(string[] strArray, string key)
{
for (int i = 0; i <= strArray.Length - 1; i++)
if (strArray[i] == key)
return true;
return false;
}
推荐答案
只需使用已经内置的 Contains() 方法:
Just use the already built-in Contains() method:
using System.Linq;
//...
string[] array = { "foo", "bar" };
if (array.Contains("foo")) {
//...
}
这篇关于字符串在数组中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文