I am making a level generation algorithm. I have all my objects stored in an array. These objects are determined by numbers. I need to check whether the number “0” exists or not. How do I do this? Thanks
3 Answers
3If you’re using an actual Array, you can use Array.Find() or Exists() to look for the matching member: Array.Find<T>(T[], Predicate<T>) Method (System) | Microsoft Learn or Array.Exists<T>(T[], Predicate<T>) Method (System) | Microsoft Learn
However, if you use a List instead, you can use List.Contains(), which has a bit easier syntax. List<T>.Contains(T) Method (System.Collections.Generic) | Microsoft Learn
you can check simple like the following :
string stringToCheck = "GHI";
string[] stringArray = { "ABC", "DEF", "GHI", "JKL" };
foreach (string x in stringArray)
{
if (x.Equals (stringToCheck))
{
MessageBox.Show("Find the string ..." + x);
}
}
Mark
Its pretty simple, use this
string[] stringArray = { "text1", "text2", "text3", "text4" };
string value = "text3";
int pos = Array.IndexOf(stringArray, value);
if (pos > -1)
{
return true;
}
else
{
return false;
}
The fastest way to search an array is doing a binary search: Array.BinarySearch [This website benchmarks other methods as well][1], but the binary search always came out on top. [1]: http://cc.davelozinski.com/c-sharp/fastest-collection-for-string-lookups
– firemyst