Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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 Im stumped! I dont know how to write VB6 code to let me find the second, third, fourth stringetc. 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.SetFocus
Text1.SelStart = where - 1
Text1.SelLength = Len(search)
Else
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 Microsofts 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, Id be very appreciative.

I thank who ever is able to help in advance.
urbaud
[1603 byte] By [Urbaud] at [2007-11-11 8:16:52]
# 1 Re: How do I find several occurances of the same string within a larger string?
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"

Do
Found = InStr(Found + 1, Text, Substring)
If Found Then
Debug.Print "'"; Substring; "' found at position"; Found
End If
Loop While Found
Phil Weber at 2007-11-11 17:25:44 >
# 2 Re: How do I find several occurances of the same string within a larger string?
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

Do
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
Loop

Of course, Phil's code is perfect and works as is!
Marco
mstraf at 2007-11-11 17:26:46 >