Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to search an Array for a Text value

Posted on 2006-07-09
7
Medium Priority
?
321 Views
Last Modified: 2010-04-23
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  
     
0
Comment
Question by:Hojoformo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 2000 total points
ID: 17068936
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
 
LVL 14

Expert Comment

by:Ramuncikas
ID: 17068946
'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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17069046
The "best" way is to use a different structure, such as a Collection or a HashTable.

Bob
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17069662
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
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17075405
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17078413
Big-O:  O(1) ;)  Thanks Jeff.

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17142892
Sometimes people just don't listen (boo-hoo)!!!

Bob
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question