DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2005
    Edmonton, Alberta, Canada

    How do I find several occurances of the same string within a larger string?

    I need some help to figure out how to find several occurrences of the same string within a larger string. I think I understand the VB6 command: Instr() as I have been successful in finding and highlighting the first of several strings within a string. But this is where I’m stumped! I don’t know how to write VB6 code to let me find the second, third, fourth string…etc. The code that I have used to find the first string is:

    Private Sub cmdFind_Click()
    Dim search, where
    search = InputBox("Enter the text you want to find”)
    where = InStr(Text1.Text, search)
    If where Then
    Text1.SelStart = where - 1
    Text1.SelLength = Len(search)
    MsgBox ("String not found")
    End If
    End Sub

    For example, if the string was "Mississippi" and I wanted to search for all of the "i"’s and then highlight them one at a time or all at once; finding all the other occurrences of the same string in the larger string. Is there a way to do it in VB6? I'm sure there is, but I don't know how.

    I guess another way to describe what I'm getting at is to use Microsoft’s Notepad as an example. I have a fair amount of text in the notepad, and at times I want to find a certain word, so I use the Find command. It will find the first occurrence of the word, and if there are other occurrences it continues and highlights each occurrence of the word until the end of the text is reached.

    If anyone out there can help me with the code, I’d be very appreciative.

    I thank who ever is able to help in advance.

  2. #2
    Join Date
    Nov 2003
    Portland, OR
    You'll need to call InStr in a loop, using its optional Start parameter to tell it to start searching after the previous occurrence of the substring. For example, to find all occurrences of 'i' in 'Mississippi':
    Dim Text As String
    Dim Substring As String
    Dim Found As Integer
    Text = "Mississippi"
    Substring = "i"
        Found = InStr(Found + 1, Text, Substring)
        If Found Then
            Debug.Print "'"; Substring; "' found at position"; Found
        End If
    Loop While Found
    Phil Weber

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Nov 2003
    Alameda, CA
    just a small note about Phil's code: in the snippet the variable Found, defined as Integer, is used also as Boolean in the two statements "if Found then" and "Loop While Found".
    This is working because VB6 internally converts Found into a boolean using the espression (Found <> 0), because in VB6 the value False of a boolean is stored as zero (True as -1)
    The code will be easier to read (and to understand), expecially if you are a beginner, by explicitely using (Found <> 0) in the boolean espressions, like for example
        Found = InStr(Found + 1, Text, Substring)
        If Found = 0 Then exit do  ''' <- in this case Not Found will NOT work
        Debug.Print "'"; Substring; "' found at position"; Found
    Of course, Phil's code is perfect and works as is!
    "There are two ways to write error-free programs. Only the third one works."

Similar Threads

  1. Re: App Object (fixes)
    By Rob Teixeira in forum .NET
    Replies: 129
    Last Post: 06-06-2002, 05:23 AM
  2. Replies: 1
    Last Post: 06-05-2001, 06:12 AM
  3. How do I detect an FTP timeout?
    By Julian Milano in forum VB Classic
    Replies: 0
    Last Post: 08-10-2000, 09:16 PM
  4. NetUserEnum Api
    By Mike in forum VB Classic
    Replies: 2
    Last Post: 04-24-2000, 03:30 PM
  5. Database problems
    By Robert Rieth in forum VB Classic
    Replies: 1
    Last Post: 04-11-2000, 03:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.