something that's fast.
How fast is fast? Ok, how many items in an array you're talking about?
Here's a small optimization to code what Ossehaas showed
Dim Src() As String
Const MyStr As String = "abc"
If TextBox1.Text.IndexOf(MyStr) < 0 Then
' Not found
Else
Src = TextBox1.Text.Split(",".ToCharArray)
' Ossehaas
For Each s As String In Src
If MyStr = s Then
' Found
Exit For
End If
Next
End If
If TextBox1.Text.IndexOf(MyStr) < 0 Then should speed up the worst case scenario i.e. the string does not exist in the array.
Array class has also Find and FindIndex methods. See from the .NET documentation, how they are defined. Then you may look at this article: Edgewaters: Using Predicates with VB.Net Array and List Find methods .
You didn't mention if the order of the items in the array must be maintained. If the order of the items is irrelevant, you can use Array.Sort(Src) and then binary search .
In theory binary search is the fastest method with O(log(n)) while the first methods are O(n2). That's the theory, not the practice ;)
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203