Searching a string array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AaronL
    New Member
    • Jan 2007
    • 99

    Searching a string array

    Hi Guys,

    I was wondering if anyone could help me. Here is the situation:

    I have an array of strings lets say from 1 to 1000. I want to write a function that allows the user to search for certain text in that array, and populate the results in a list box. Just like your standard search engine, given the option to put & for multiple words etc... I know it involves the InStr() function, but I'm unsure of the actual logic of the code I will want to use.

    Thanks!
  • VBWheaties
    New Member
    • Feb 2008
    • 145

    #2
    Originally posted by chelf
    Hi Guys,

    I was wondering if anyone could help me. Here is the situation:

    I have an array of strings lets say from 1 to 1000. I want to write a function that allows the user to search for certain text in that array, and populate the results in a list box. Just like your standard search engine, given the option to put & for multiple words etc... I know it involves the InStr() function, but I'm unsure of the actual logic of the code I will want to use.

    Thanks!
    Heres two functions that should give you an idea of how I would do it.
    You can pass in 1 or more strings in FindStrings.
    For each value in the Paramarray, call IsInArray to return a boolean if the string exists in the array.

    Code:
    Public Function FindStrings(ParamArray Searches() As String) As Boolean
       Dim i As Integer
       For i = 0 to UBOUND(Searches) - 1
          If IsInArray(Searches(i)) = FALSE Then 
             FindStrings = False
             Exit Function 
          End If 
       Next 
       FindStrings = True
    End Function 
    Public Function IsInArray(szSearch As String) as Boolean
       'ToDo: Write code to iterate array and look for szSearch. 
    End Function

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Whether you use the Instr() function or not depends on the type of match you're looking for. If you only want to check an entire string to see whether it matches, you would just use the = operator. If you want to see whether your string appears anywhere within another string, then you would probably use Instr() to test it.

      On the other hand, you might use the Like operator to allow wildcard matching. Have a look at this example...
      [CODE=vb]Dim a As String, b As String, c As String
      a = "ABCDEFG"
      b = "ABC*"
      c = "ABC"
      Debug.Print "B:"; (a Like b)
      Debug.Print "C:"; (a Like c)
      [/CODE]

      Comment

      Working...