我有一个字符串数组:
string[] stringArray = { "text1", "text2", "text3", "text4" };
string value = "text3";
我想确定 stringArray
中是否包含 value
。如果是的话,我想找出它在数组中的位置。
我不想使用循环。有谁能建议我如何做到这一点?
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)
我们还可以使用 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"));
Array.Exists(array, element => element.Equals("perl", StringComparison.OrdinalIgnoreCase)
。 - Topher Birth编辑:我没注意到你也需要位置。由于它是显式实现的,所以您不能直接在数组类型的值上使用 IndexOf
。但是,您可以使用以下方法:
IList<string> arrayAsList = (IList<string>) stringArray;
int index = arrayAsList.IndexOf(value);
if (index != -1)
{
...
}
(这类似于按照Darin的答案调用Array.IndexOf
- 只是另一种方法。我不清楚为什么在数组中明确实现了IList<T>.IndexOf
,但无论如何...)
if (months.Contains(model.Month))
。 - Jon Skeetmodel.contains(months[x])
。 - Murali Murugesan您可以使用 Array.IndexOf()
- 注意如果元素未找到,它将返回-1,您需要处理此情况。
int index = Array.IndexOf(stringArray, value);
string [] arr = {"One","Two","Three"};
var target = "One";
var results = Array.FindAll(arr, s => s.Equals(target));
使用 System.Linq
stringArray.Contains(value3);
检查数组中是否包含给定值的最佳方法是使用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。因为它是显式(私有)实现,所以在使用这些方法之前需要先将数组强制转换。这不仅适用于字符串 - 只要元素的类实现了 IComparable,你就可以使用此技巧来检查任何类型的数组是否包含任何元素。
请注意,并非所有的 IList<T>
方法都是这样工作的。试图在数组上使用 IList<T>
的 Add 方法将会失败。
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");
}
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); }
}
Array.IndexOf
是否区分大小写?"text1" == "TEXT1"
是否相等? - E.V.I.L.Array.IndexOf
returns−1
only if the index is 0-bounded. This case would break, so be aware!var a = Array.CreateInstance(typeof(int),new int[] { 2 }, new int[] { 1 }); a.SetValue(1, 1); Console.WriteLine(Array.IndexOf(a, 26)); // 0
- benscabbia