|
-
Nov 6th, 2006, 02:33 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Searching in a text file with array of words
Hi, normally I'm using the code below to search for words in a text file and display the entire line when a match has been found.
VB Code:
Private Sub Command1_Click()
Dim sLines As String
Open "C:\textfile.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, sLines
If InStr(1, sLines, "horse") And InStr(1, sLines, "fish") Then
Text1.Text = Text1.Text & sLines & vbCrLf
End If
DoEvents
Loop
Close #1
End Sub
How can I do the same thing when I have the words (horse and fish) stored in an array? Normally I can replace "Horse" with strArray(0) and "Fish" with strArray(1), but the number of words in the array is always different.
In other words, how do I use an array with "If InStr(1, sLines, ????????) Then" when the amount of search words is always different?
(btw, the text file can be 50MB big)
-
Nov 6th, 2006, 03:04 PM
#2
Re: Searching in a text file with array of words
You need to use a loop to go thru all of the items, and store the result of the checks - if any fail then it isn't a match, so don't add the text.
VB Code:
Dim booIsFound as Boolean
Dim lngCounter as Long
For lngCounter = 0 To UBound(strArray)
booIsFound = (InStr(1, sLines, strArray(lngCounter)) > 0)
'the line above is the same as:
'If InStr(1, sLines, strArray(lngCounter)) Then
' booIsFound = True
'Else
' booIsFound = False
'End If
If Not booIsFound Then Exit For 'no point checking the others!
Next lngCounter
If booIsFound Then
Text1.Text = Text1.Text & sLines & vbCrLf
End If
-
Nov 7th, 2006, 01:00 AM
#3
Thread Starter
Frenzied Member
Re: Searching in a text file with array of words
Thank you
-
Nov 7th, 2006, 09:17 AM
#4
Junior Member
Re: [RESOLVED] Searching in a text file with array of words
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|