Results 1 to 13 of 13

Thread: String Comparisons

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    181

    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

  2. #2
    Addicted Member Quintonir's Avatar
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    155

    Post 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

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. Dim strTemp$, StringToLookIn$
    2. Dim arrTemp()
    3.  
    4. Open "C:\text.txt" For Input As #1
    5.   strTemp = Input(LOF(1),1)
    6. Close #1
    7.  
    8.   StringToLookIn = Text1.Text
    9.  
    10.   arrTemp = Split(strTemp,vbCrLf)
    11.  
    12.   For x = 0 To UBound(arrTemp)
    13.     If InStr(1,StringToLookIn,arrTemp(x)) <> 0 Then
    14.       MsgBox arrTemp(x) & " found in string at least once."
    15.     End If
    16.   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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    181
    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?

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    181
    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

  8. #8
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
    2. 'Author: Aaron Young
    3. 'Origin: -
    4. 'Purpose: Split Function
    5. 'Version: VB4+
    6.     Dim sParts() As String
    7.     Dim lParts As Long
    8.     Dim lPos As Long
    9.    
    10.     lPos = InStr(sString, sSeparator)
    11.     While lPos
    12.         ReDim Preserve sParts(lParts)
    13.         sParts(lParts) = Left(sString, lPos - 1)
    14.         sString = Mid(sString, lPos + Len(sSeparator))
    15.         lPos = InStr(sString, sSeparator)
    16.         lParts = lParts + 1
    17.     Wend
    18.     If Len(sString) Then
    19.         ReDim Preserve sParts(lParts)
    20.         sParts(lParts) = sString
    21.     End If
    22.     Split2 = IIf(lParts, sParts, Array())
    23. End Function
    24.  
    25.  
    26. Private Sub Command1_Click()
    27.  
    28.     Dim vArray As Variant
    29.     Dim sTemp As String, sTemp2 As String
    30.     Dim strText As String
    31.  
    32.     Open "C:\Test.txt" For Input As #1
    33.         strText = Input(LOF(1), 1)
    34.     Close #1
    35.    
    36.     vArray = Split2(strText, vbCrLf)
    37.  
    38.     sTemp = vArray(0)
    39.     sTemp2 = vArray(1)
    40.  
    41. 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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    181
    Cool!

    That works (well, if I use the first Command1_Click code that you posted instead of that last one )

    Thanx

  10. #10
    Megatron
    Guest
    Or you can just use the InStr function.
    VB Code:
    1. Open "C:\text.txt" For Input As #1
    2.   strTemp = Input(LOF(1),1)
    3. Close #1
    4.  
    5. If InStr(1, strTemp, "MyWord") <> 0 Then MsgBox "String found"

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    181
    How did you guys get the code to say "visual basic code" and the colouring instead of just "code"?

  12. #12
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    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

  13. #13
    Addicted Member Quintonir's Avatar
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    155

    Post 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
  •  



Click Here to Expand Forum to Full Width