Problem With String Manipulation in VB6

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ThatVBGuy
    New Member
    • Oct 2006
    • 5

    #1

    Problem With String Manipulation in VB6

    Hello All,

    I could really use some help with this problem its driving me nuts. I have a small vb app, the goal of the app is to read an html doc into a variable then go through that variable and find and replace some tags. I have 3 functions. 1 to open the doc, the 2nd to find and replace the tags the 3rd to save the info. the code is pasted below :

    Code:
    Public Function ReadFileContents(FileFullPath As String) As _
       String
    On Error GoTo ErrorHandler
    Dim iFileNumber As Integer
    Dim sAns As String
    If Dir(FileFullPath) = "" Then Exit Function
    iFileNumber = FreeFile
    Open FileFullPath For Input As #iFileNumber
    sAns = Input(LOF(iFileNumber), #iFileNumber)
    sAns = UCase(sAns)
    ReadFileContents = sAns
    'MsgBox sAns
    ErrorHandler:
    Close #iFileNumber
    End Function
    Code:
    Public Function ReplaceText(ByVal txt As String, ByVal _
        from_str As String, ByVal to_str As String) As String
    Dim result As String
    Dim from_len As Integer
    Dim pos As Integer
        from_len = Len(from_str)
        Do While Len(txt) > 0
            ' Find level 1.
            pos = InStr(txt, from_str)
            If pos = 0 Then
                ' No more occurrences.
                result = result & txt
                txt = ""
            Else
                ' Make the replacement.
                result = result & Left$(txt, pos - 1) & to_str
                txt = Mid$(txt, pos + from_len)
            End If
        Loop
        ReplaceText = result
    End Function
    Code:
    Public Function SaveTextToFile(FileFullPath As String, _
     sText As String, Optional Overwrite As Boolean = True) As _
     Boolean
        
    On Error GoTo ErrorHandler
    Dim iFileNumber As Integer
    iFileNumber = FreeFile
    
    If Overwrite Then
        Open FileFullPath For Output As #iFileNumber
    Else
        Open FileFullPath For Append As #iFileNumber
    End If
    
    Print #iFileNumber, sText
    SaveTextToFile = True
    
    ErrorHandler:
    Close #iFileNumber
    
    End Function
    Code:
    Private Sub Form_Load()
    '****************************************************************
    'Define variables for strings
    Dim sFileText As String
    Dim sFrom1 As String, sTo1 As String
    '****************************************************************
    
    'Open the HTML file as text
    sFileText = ReadFileContents("c:\toc\toc.htm")
    
    '****************************************************************
    'In this section you should setup the strings you are searching
    'The document for. You should also setup the replacement string
    'If the above named strings are found
    '****************************************************************
    sFrom1 = "<img src=" + Chr(34) + "space.gif" + Chr(34)
    MsgBox sFrom1
    sTo1 = "<div class=" + Chr(34) + "toc1" + Chr(34) + "><img src=" + Chr(34) + "space.gif" + Chr(34)
    MsgBox sTo1
    
    Call ReplaceText(sFileText, sFrom1, sTo1)
    Call SaveTextToFile("c:\toc\toc.htm", sFileText, True)
    
    End Sub
    now here is the problem. When i hit the messagebox statements the variable is displayed in the way i want it to be. yet when i step through the logic before the call to ReplaceText when i check the value of sFrom1 and sTo1 there are extra quotes and things that arent allowing the string to be found and thus not replaced. I would like to know how to pass the variable into the function in the way its being displayed as opposed to this other way with all the additional quotes.

    all help is appreciated im stuck here. I would like someone to help me but also explain why im getting this problem in the solution. I dont just want an answer i would like to learn as well.

    thanks in advance,
    john
  • ThatVBGuy
    New Member
    • Oct 2006
    • 5

    #2
    any ideas?

    Comment

    • PEB
      Recognized Expert Top Contributor
      • Aug 2006
      • 1418

      #3
      Hi man,

      In Access Basic there is a function replace(Your string, searched, replace with), can you see about the same function in VB6?

      If there is this kind of function try with it and say if there is a pb!

      In fact it's also interesting the way that you obtain your html string !

      Comment

      • ThatVBGuy
        New Member
        • Oct 2006
        • 5

        #4
        Hey thanks for the input. I tried using the vb search and replace function the problem im running into is this. In my code im using sFrom1 and sTo1 as the variables for the string to find and the string to replace it with. When i check the variable values with a msgbox on the sFrom1 variable it looks like the string im looking for HOWEVER when i pass that value into my function or the standard vb replace function it passes it in with a slew of extra quotation marks and i have no idea why. If i could get it to pass the value in i see when i display that variable in a msgbox i would be fine. Any ideas?

        Comment

        • psnanu
          New Member
          • Sep 2007
          • 3

          #5
          Hi,
          Just change
          call ReplaceText(sFi leText, sFrom1, sTo1)
          to
          sFileText = ReplaceText(sFi leText, UCase(sFrom1), sTo1)

          This should work for you.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by psnanu
            Just change ...
            Thanks for that. It might be a bit late to help ThatVBGuy, though. According to their profile, he/she hasn't dropped in for about four months.

            Comment

            Working...