Hojoformo
asked on
How to search an Array for a Text value
Quick question. What is the best approach to search an array for a text string string. I have 2 array's. I loop through an array filenames and for each file name I want to see if it exists on another array. The second array only has four columns and the column I need to search is the 4th column. Very simple code as follows:
'Array 1
Dim fileEntries() As String = System.IO.Directory.GetFil es(TextBox 1.Text)
Dim filename As String
'Array 2
Dim Fields() As String
Fields = Str.Split(","c)
For Each filename In fileEntries
??? What is the best way to search the Fields(3) array column 4 using filename.
Next
'Array 1
Dim fileEntries() As String = System.IO.Directory.GetFil
Dim filename As String
'Array 2
Dim Fields() As String
Fields = Str.Split(","c)
For Each filename In fileEntries
??? What is the best way to search the Fields(3) array column 4 using filename.
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The "best" way is to use a different structure, such as a Collection or a HashTable.
Bob
Bob
I agree with Bob...
A Binary Search is only better than an iterative (For...Next) search if the items in the array are ALREADY sorted.
A Binary Search is only better than an iterative (For...Next) search if the items in the array are ALREADY sorted.
HashTable (or System.Collections.Generic .Dictionar y) are nice, since you can use the .ContainsKey(keyName) to determine if the key already exists -- without searching.
Big-O: O(1) ;) Thanks Jeff.
Bob
Bob
Sometimes people just don't listen (boo-hoo)!!!
Bob
Bob
Dim fileEntries() As String = System.IO.Directory.GetFil
Dim filename As String
'Array 2
Dim Fields() As String
Fields = Str.Split(","c)
For Each filename In fileEntries
If filename = Fields(3) Then
'I found it
End If
Next