Searching an Array of String with a wildcard

I was trying to find an occurrance of a string in an array. If an array of string contains:
a1_123
a2_sdf
a3_hhh
b1_123
b2_gre

and the need is to find the first occurrance of “b1_”, is there a way to do this using IndexOf?

I tried using a wildcard like:
ind=MyArray.IndexOf(“b1_*”) and ind=MyArray.IndexOf(“b1_%”), but ind always = -1.

Am I using the incorrect wildcard character or the wildcard character incorrectly, or is looping through the array with BeginsWith the best way?

Thanks

There aren’t any wildcard characters. .IndexOf(“b1_”) will find the first one. If you want to find them all, one by one, use the returned index position as the starting point for your next search for “b1_”.

I should have said, knowing only that an entry starts with “b1_”, I would like to get the position of “b1_123” in the case of the example above.

ind=MyArray.IndexOf(“b1_”) returns -1 on the data.

If you’re looking for results where the string always starts with the search parameter:

Public Function findStartsWith(inArray() as String, startsWith as String) As Integer
  var max as Integer = inArray.LastIndex
  for index as Integer = 0 to max
    if inArray(index).Left( startsWith.Length ) = startsWith then Return index
  next
  
  Return -1
End Function

Or for matching anywhere within the strings:

Public Function findContains(inArray() as String, criteria as String) As Integer
  var max as Integer = inArray.LastIndex
  for index as Integer = 0 to max
    if inArray(index).IndexOf( criteria ) >= 0 then Return index
  next
  
  Return -1
End Function

Use as:

var strings() as String = Array( "a1_123", "a2_sdf", "a3_hhh", "b1_123", "b2_gre" )

'// Starts with
var firstInstance as Integer = findStartsWith( strings, "b1_" )
if firstInstance >= 0 then
  MessageBox( strings(firstInstance) )
end if

'// Contains
firstInstance = findContains( strings, "b1_" )
if firstInstance >= 0 then
  MessageBox( strings(firstInstance) )
end if

Got it. Examining the elements is the way.

Generalized method:

Public Function ArrayStr_FindIndex(findArray() as string, findWord as string, optional searchType as Integer) As integer
  '  0=begins with, 1=contains, 2=ends with
  
  Var index As Integer
  Var word As String
  
  Select Case searchType 
  Case 0
    For Each word In findArray
      If word.BeginsWith(findWord) Then
        Return index
      End If
      index=index+1
    Next
    
  Case 1
    For Each word In findArray
      If word.Contains(findWord) Then
        Return index
      End If
      index=index+1
    Next
    
  Case 2
    For Each word In findArray
      If word.EndsWith(findWord) Then
        Return index
      End If
      index=index+1
    Next
    
  End Select
  
  Return -1
End Function