检查一个string数组是否包含一个值,如果是的话,得到它的位置

我有这个string数组:

string[] stringArray = { "text1", "text2", "text3", "text4" }; string value = "text3"; 

我想确定如果stringArray包含value 。 如果是这样,我想find它在arrays中的位置。

我不想使用循环。 任何人都可以build议我可以这样做吗?

你可以使用Array.IndexOf方法:

 string[] stringArray = { "text1", "text2", "text3", "text4" }; string value = "text3"; int pos = Array.IndexOf(stringArray, value); if (pos > -1) { // the array contains the string and the pos variable // will have its position in the array } 
 var index = Array.FindIndex(stringArray, x => x == value) 

编辑:我没有注意到你也需要这个位置。 您不能直接在数组types的值上使用IndexOf ,因为它是显式实现的。 但是,您可以使用:

 IList<string> arrayAsList = (IList<string>) stringArray; int index = arrayAsList.IndexOf(value); if (index != -1) { ... } 

(这与根据Darin的答案调用Array.IndexOf类似 – 只是一种替代方法。我不清楚为什么IList<T>.IndexOf 在数组中明确实现的,但是不介意…)

您可以使用Array.IndexOf() – 请注意,如果元素未find,它将返回-1,您必须处理这种情况。

 int index = Array.IndexOf(stringArray, value); 

你可以尝试像这样…你可以使用Array.IndexOf(),如果你想知道的位置也

  string [] arr = {"One","Two","Three"}; var target = "One"; var results = Array.FindAll(arr, s => s.Equals(target)); 

我们也可以使用Exists

 string[] array = { "cat", "dog", "perl" }; // Use Array.Exists in different ways. bool a = Array.Exists(array, element => element == "perl"); bool c = Array.Exists(array, element => element.StartsWith("d")); bool d = Array.Exists(array, element => element.StartsWith("x")); 

你可以试试这个,它查找包含这个元素的索引,并且将索引号设置为int,然后检查int是否大于-1,所以如果它是0或者更大,那么意味着它find了这样的索引 – 作为数组是基于0的。

 string[] Selection = {"First", "Second", "Third", "Fourth"}; string Valid = "Third"; // You can change this to a Console.ReadLine() to //use user input int temp = Array.IndexOf(Selection, Valid); // it gets the index of 'Valid', // in our case it's "Third" if (temp > -1) Console.WriteLine("Valid selection"); } else { Console.WriteLine("Not a valid selection"); } 

IMO检查数组是否包含给定值的最佳方法是使用System.Collections.Generic.IList<T>.Contains(T item)方法如下:

 ((IList<string>)stringArray).Contains(value) 

完整的代码示例:

 string[] stringArray = { "text1", "text2", "text3", "text4" }; string value = "text3"; if (((IList<string>)stringArray).Contains(value)) Console.WriteLine("The array contains "+value); else Console.WriteLine("The given string was not found in array."); 

T[]数组私下实现了List<T>的几个方法,如Count和Contains。 因为它是一个明确的(私有)实现,所以你不能使用这些方法而不用先转换数组。 这不仅适用于string – 只要元素的类实现IComparable,就可以使用这个技巧来检查任何types的数组是否包含任何元素。

请记住,并不是所有的IList<T>方法都是这样工作的。 尝试在数组上使用IList<T>的Add方法将失败。

 string x ="Hi ,World"; string y = x; char[] whitespace = new char[]{ ' ',\t'}; string[] fooArray = y.Split(whitespace); // now you have an array of 3 strings y = String.Join(" ", fooArray); string[] target = { "Hi", "World", "VW_Slep" }; for (int i = 0; i < target.Length; i++) { string v = target[i]; string results = Array.Find(fooArray, element => element.StartsWith(v, StringComparison.Ordinal)); // if (results != null) { MessageBox.Show(results); } } 

我创build了一个重用的扩展方法。

  public static bool InArray(this string str, string[] values) { if (Array.IndexOf(values, str) > -1) return true; return false; } 

如何调用它:

 string[] stringArray = { "text1", "text2", "text3", "text4" }; string value = "text3"; if(value.InArray(stringArray)) { //do something } 

最简单,最简单的方法如下。

 string[] stringArray = { "text1", "text2", "text3", "text4" }; string value = "text3"; if(stringArray.Contains(value)) { // Do something if the value is available in Array. } 
 string[] strArray = { "text1", "text2", "text3", "text4" }; string value = "text3"; if(Array.contains(strArray , value)) { // Do something if the value is available in Array. }