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
}
string[] stringArray = { "text1", "text2", "text3", "text4" };
string value = "text3";
if(stringArray.Contains(value))
{
// Do something if the value is available in Array.
}
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.");
string[] strArray = { "text1", "text2", "text3", "text4" };
string value = "text3";
if(Array.contains(strArray , value))
{
// Do something if the value is available in Array.
}
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); }
}
你可以尝试这样做,它查找包含这个元素的索引,它将索引号设置为 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");
}