You can find string in an array using two methods. (I know there are plenty of methods, but two methods are most common)
Method 1
You can loop each item of an array and compare each item of the array with the items that are to find. If match found it return true otherwise it returns false.
1 2 3 4 5 6 7 8 9 10 11 |
public bool isInArray(string[] array, string text) { foreach (string item in array) { if (item == text) { return true; } } return false; } |
Method 2
Array.IndexOf is used to find items in an array.
According to MSDN, Array.IndexOf returns the index of the first occurrence of a value in a one-dimensional Array or in a portion of the Array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public bool isInArray(string text) { string[] stringArray = { "text1", "text2", "text3", "text4" }; string value = "text3"; int pos = Array.IndexOf(stringArray, value); if (pos > -1) { return true; } else { return false; } } |