Link to home
Start Free TrialLog in
Avatar of tw_chase
tw_chase

asked on

Instr Like Function in Visual Basic 6

I need to know how to get the starting position in a string using masks you use in LIKE functions.  Let me clarify.

Say I have a string like "ABCDEFG123MNOPQ"

I want the starting position where the string is like ###.  I don't want any Mid functions because that could take a long time to scan huge strings position by position.  Does VB6 have a built in function or is there another way?

Instr("ABCDEFG123MNOPQ","123") will return my starting postion

"ABCDEFG123MNOPQ" LIKE "*###*" will return if that mask combination exists.

I need the combination of both.

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I agree with angel, regular expressions would definately be the way to go, he deserves the majority if not all of the points for it.  If it helps, heres an example for you:

Sub tw_chase()
 Dim RegEx As Object, RegM As Object, tempStr As String, tempStr2 As String
 Set RegEx = CreateObject("vbscript.regexp")
 RegEx.Pattern = "\d{3}"
 tempStr = "ABCDEFG123MNOPQ"
 tempStr2 = "as2lkasdfo34klalsd872alsd"
 If RegEx.Test(tempStr) Then
  Set RegM = RegEx.Execute(tempStr).Item(0)
  MsgBox "Original string: " & tempStr & vbCrLf & _
         """" & RegM & """ found at position " & RegM.FirstIndex + 1
 End If
 If RegEx.Test(tempStr2) Then
  Set RegM = RegEx.Execute(tempStr2).Item(0)
  MsgBox "Original string: " & tempStr2 & vbCrLf & _
         """" & RegM & """ found at position " & RegM.FirstIndex + 1
 End If
 Set RegEx = Nothing
 Set RegM = Nothing
End Sub

Matt