|
-
Jun 10th, 2001, 07:31 AM
#1
Thread Starter
Addicted Member
String Comparisons
Whats the best way to get a list of words from a text file, each on a seperate line, and then searching a string for them?
Thanx
-
Jun 10th, 2001, 08:36 AM
#2
Addicted Member
Sample Code
I'm very busy right now, but I'll try to provide a sample code which loads the contents of a *.txt and then searches for a specific string.
Quintonir
-
Jun 10th, 2001, 09:59 AM
#3
VB Code:
Dim strTemp$, StringToLookIn$
Dim arrTemp()
Open "C:\text.txt" For Input As #1
strTemp = Input(LOF(1),1)
Close #1
StringToLookIn = Text1.Text
arrTemp = Split(strTemp,vbCrLf)
For x = 0 To UBound(arrTemp)
If InStr(1,StringToLookIn,arrTemp(x)) <> 0 Then
MsgBox arrTemp(x) & " found in string at least once."
End If
Next x
is that kind of what you are looking for?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 10th, 2001, 10:25 AM
#4
Thread Starter
Addicted Member
The code looks fine, but it produces an error when running the line:
Code:
arrTemp = Split(strTemp,vbCrLf)
The error is:
"Compile Error: array or user-defined type expected"
Any ideas?
-
Jun 10th, 2001, 10:28 AM
#5
not sure why...try changing "Dim arrTemp()" to "Dim arrTemp As Variant"
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 10th, 2001, 10:31 AM
#6
wait, no that's not it. You are using VB5, and I didn't realize that. There is no Split function in version 5. give me a minute and I will find the code that simulates the Split function
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 10th, 2001, 10:31 AM
#7
Thread Starter
Addicted Member
Could the problem be that I'm not using the VB6 split function, as I'm on VB5, and am using this instead:
Code:
Sub Split2(ByRef arr() As String, ByVal strInput As String, ByVal strDelimit As String)
Dim i, tmp
ReDim arr(0)
For i = 1 To Len(strInput)
tmp = Mid(strInput, i, 1)
If tmp = strDelimit Then
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = ""
Else
arr(UBound(arr)) = arr(UBound(arr)) & tmp
End If
Next i
End Sub
-
Jun 10th, 2001, 10:33 AM
#8
VB Code:
Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
'Author: Aaron Young
'Origin: -
'Purpose: Split Function
'Version: VB4+
Dim sParts() As String
Dim lParts As Long
Dim lPos As Long
lPos = InStr(sString, sSeparator)
While lPos
ReDim Preserve sParts(lParts)
sParts(lParts) = Left(sString, lPos - 1)
sString = Mid(sString, lPos + Len(sSeparator))
lPos = InStr(sString, sSeparator)
lParts = lParts + 1
Wend
If Len(sString) Then
ReDim Preserve sParts(lParts)
sParts(lParts) = sString
End If
Split2 = IIf(lParts, sParts, Array())
End Function
Private Sub Command1_Click()
Dim vArray As Variant
Dim sTemp As String, sTemp2 As String
Dim strText As String
Open "C:\Test.txt" For Input As #1
strText = Input(LOF(1), 1)
Close #1
vArray = Split2(strText, vbCrLf)
sTemp = vArray(0)
sTemp2 = vArray(1)
End Sub
its from Aaron Young, and it works
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 10th, 2001, 10:37 AM
#9
Thread Starter
Addicted Member
Cool! 
That works (well, if I use the first Command1_Click code that you posted instead of that last one )
Thanx
-
Jun 10th, 2001, 10:50 AM
#10
Or you can just use the InStr function.
VB Code:
Open "C:\text.txt" For Input As #1
strTemp = Input(LOF(1),1)
Close #1
If InStr(1, strTemp, "MyWord") <> 0 Then MsgBox "String found"
-
Jun 10th, 2001, 10:53 AM
#11
Thread Starter
Addicted Member
How did you guys get the code to say "visual basic code" and the colouring instead of just "code"?
-
Jun 10th, 2001, 11:01 AM
#12
Originally posted by adamcox
How did you guys get the code to say "visual basic code" and the colouring instead of just "code"?
use [ vbcode ][ /vbcode ] (without the spaces) instead of [ code ][ /code ] (without the spaces)
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Jun 10th, 2001, 11:14 AM
#13
Addicted Member
Result Code
I haven't fully tested this code, so if it runs in error or doesn't reach its goal, please notice me.
I know the function can be shortened. The function does much more than needed, but that's to show you some things. It loads the contents of the file into an array, and then scans the array for matches, listing the line numbers in another array.
The function:
Code:
Private Function SearchFile(ByRef FilePath As String, ByRef SearchString As String, ByRef WholeStory As String) As Variant
Dim sLineStr() As String
Dim lFndLine() As Long
Dim iFFile As Integer
Dim lNumLine As Long
Dim lCount As Long
Dim lNumFnd As Long
Dim sWholeStory As String
On Error GoTo erSrcFile
If Dir(FilePath) = "" Then GoTo erSrcFile
iFFile = FreeFile
Open FilePath For Input As #iFFile
Do While Not EOF(iFFile)
lNumLine = lNumLine + 1
ReDim Preserve sLineStr(1 To lNumLine)
Line Input #iFFile, sLineStr(lNumLine)
Loop
Close #iFFile
For lCount = 1 To lNumLine
If InStr(sLineStr(lCount), SearchString) > 0 Then
lNumFnd = lNumFnd + 1
ReDim Preserve lFndLine(1 To lNumFnd)
lFndLine(lNumFnd) = lCount
End If
sWholeStory = sWholeStory & sLineStr(lCount) & vbNewLine
Next
sWholeStory = Left(sWholeStory, Len(sWholeStory) - Len(vbNewLine))
WholeStory = sWholeStory
SearchFile = lFndLine
Exit Function
erSrcFile:
SearchFile = Null
End Function
Usage:
Code:
Private Sub Test()
Dim sFilePath As String
Dim sSearchStr As String
Dim lLineStr As Variant
Dim sWholeStory As String
sFilePath = "C:\Addresses.txt"
sSearchStr = "Avenue"
lLineStr = SearchFile(sFilePath, sSearchStr, sWholeStory)
If VarType(sLineStr) = 1 Then MsgBox "An error occured!", vbExclamation, "Error": Exit Sub
'lLineStr now contains an array with the lines containing sSearchStr
'sWholeStory now contains the complete content of sFilePath
End Sub
The code isn't very usefull, and again takes a long road to reach its goal. But in this way you can see how it is done. And I hope it solves your problem.
Quintonir
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
|