How to check array contents
-
Hello Experts!!! I am declaring array arr() as string assign values to array...now i want to search specific content in that array.Can anybody tell me how will i do this?
If you want to search for a String within an array of Strings you can use this function.
Private Function indexInArray(ByVal theArray As String(), ByVal value As String) As Integer For i As Integer = 0 To theArray.Length - 1 If theArray(i) = value Then Return i Next Return -1 End Function
To use this function you have to pass the array you want the string to be searched in and the string you want to search (value). This function will return -1 if it doesn't find the string you want to search within the array. If it does find it it returns the index. NOTE: This only works for one-dimensional arrays. You can use this sub to test it.
Private Sub testIt() Dim myStrings(1) As String myStrings(0) = "hello" myStrings(1) = "world" If indexInArray(myStrings, "hello") <> -1 Then MessageBox.Show("The string in contained within the array") End If End Sub
-
If you want to search for a String within an array of Strings you can use this function.
Private Function indexInArray(ByVal theArray As String(), ByVal value As String) As Integer For i As Integer = 0 To theArray.Length - 1 If theArray(i) = value Then Return i Next Return -1 End Function
To use this function you have to pass the array you want the string to be searched in and the string you want to search (value). This function will return -1 if it doesn't find the string you want to search within the array. If it does find it it returns the index. NOTE: This only works for one-dimensional arrays. You can use this sub to test it.
Private Sub testIt() Dim myStrings(1) As String myStrings(0) = "hello" myStrings(1) = "world" If indexInArray(myStrings, "hello") <> -1 Then MessageBox.Show("The string in contained within the array") End If End Sub
-
Thanx for reply but in function If theArray(i) = value Then Return i //doesn't return i after finding index value same with value,it return -1 for all values
How exactly are you using the function? It should be :
indexInArray(the array you are working with, "value to search")
-
Hello Experts!!! I am declaring array arr() as string assign values to array...now i want to search specific content in that array.Can anybody tell me how will i do this?
Hi, there is an Array.Find(Of T) method in recent .NET versions. You can give it a predicate that explains what exactly is a find. I haven't used it yet. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hello Experts!!! I am declaring array arr() as string assign values to array...now i want to search specific content in that array.Can anybody tell me how will i do this?