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.GetFiles(TextBox1.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  
     
HojoformoAsked:
Who is Participating?
 
ZeonFlashConnect With a Mentor Commented:
You can try using the BinarySearch method on your array, but since I don't have my dev machine in front of me, I can't test it.  I'm 99% sure that you can search for strings using it.

-----
'Sort the array first, since a binary search performs optimally on a sorted list
Array.Sort(Fields(3))
Dim index As Integer

For Each filename In fileEntries
     index = Array.BinarySearch(Fields(3), filename)
Next  
------

As I said, I can't test any of that out, but it might get you started in the right direction.
0
 
RamuncikasCommented:
'Array 1
Dim fileEntries() As String = System.IO.Directory.GetFiles(TextBox1.Text)
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  
     
0
 
Bob LearnedCommented:
The "best" way is to use a different structure, such as a Collection or a HashTable.

Bob
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
Jeff CertainCommented:
HashTable (or System.Collections.Generic.Dictionary) are nice, since you can use the .ContainsKey(keyName) to determine if the key already exists -- without searching.
0
 
Bob LearnedCommented:
Big-O:  O(1) ;)  Thanks Jeff.

Bob
0
 
Bob LearnedCommented:
Sometimes people just don't listen (boo-hoo)!!!

Bob
0
All Courses

From novice to tech pro — start learning today.