Instr Like Function in Visual Basic 6

Posted on 2006-05-18
Last Modified: 2008-02-20
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.
Question by:tw_chase
    LVL 142

    Accepted Solution

    LVL 35

    Expert Comment

    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


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now