检查字符串数组是否包含值,如果包含,则获取其位置

我有一个字符串数组:

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

我想确定 stringArray是否包含 value。如果包含,我想定位它在数组中的位置。

我不想用循环,有人能告诉我该怎么做吗?

486931 次浏览

编辑: 我没注意到你也需要这个职位。不能直接对数组类型的值使用 IndexOf,因为它是显式实现的。不过,你可使用:

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

(根据 Darin 的回答,这类似于调用 Array.IndexOf——只是一种替代方法。我不清楚为什么 IList<T>.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
}

您可以使用 Array.IndexOf()-注意,如果没有找到元素并且您必须处理这种情况,那么它将返回 -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));
var index = Array.FindIndex(stringArray, x => x == value)

最简单、最短的方法如下。

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


if(stringArray.Contains(value))
{
// Do something if the value is available in Array.
}

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。因为它是一个显式(私有)实现,所以如果不首先强制转换数组,就无法使用这些方法。这不仅仅适用于字符串-只要元素的类实现了 ICompable,您就可以使用这个技巧来检查任何类型的数组是否包含任何元素。

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

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


if(Array.contains(strArray , value))
{
// Do something if the value is available in Array.
}

我们也可以使用 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"));
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); }


}

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

   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
}

你可以尝试这样做,它查找包含这个元素的索引,它将索引号设置为 int,然后检查 int 是否大于 -1,所以如果它是0或更大,那么它就意味着它找到了这样一个索引-因为数组是基于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");
}

使用系统

stringArray.Contains(value3);