469,913 Members | 2,648 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

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 :

Expand|Select|Wrap|Line Numbers
  1. Public Function ReadFileContents(FileFullPath As String) As _
  2.    String
  3. On Error GoTo ErrorHandler
  4. Dim iFileNumber As Integer
  5. Dim sAns As String
  6. If Dir(FileFullPath) = "" Then Exit Function
  7. iFileNumber = FreeFile
  8. Open FileFullPath For Input As #iFileNumber
  9. sAns = Input(LOF(iFileNumber), #iFileNumber)
  10. sAns = UCase(sAns)
  11. ReadFileContents = sAns
  12. 'MsgBox sAns
  13. ErrorHandler:
  14. Close #iFileNumber
  15. End Function
Expand|Select|Wrap|Line Numbers
  1. Public Function ReplaceText(ByVal txt As String, ByVal _
  2.     from_str As String, ByVal to_str As String) As String
  3. Dim result As String
  4. Dim from_len As Integer
  5. Dim pos As Integer
  6.     from_len = Len(from_str)
  7.     Do While Len(txt) > 0
  8.         ' Find level 1.
  9.         pos = InStr(txt, from_str)
  10.         If pos = 0 Then
  11.             ' No more occurrences.
  12.             result = result & txt
  13.             txt = ""
  14.         Else
  15.             ' Make the replacement.
  16.             result = result & Left$(txt, pos - 1) & to_str
  17.             txt = Mid$(txt, pos + from_len)
  18.         End If
  19.     Loop
  20.     ReplaceText = result
  21. End Function
Expand|Select|Wrap|Line Numbers
  1. Public Function SaveTextToFile(FileFullPath As String, _
  2.  sText As String, Optional Overwrite As Boolean = True) As _
  3.  Boolean
  5. On Error GoTo ErrorHandler
  6. Dim iFileNumber As Integer
  7. iFileNumber = FreeFile
  9. If Overwrite Then
  10.     Open FileFullPath For Output As #iFileNumber
  11. Else
  12.     Open FileFullPath For Append As #iFileNumber
  13. End If
  15. Print #iFileNumber, sText
  16. SaveTextToFile = True
  18. ErrorHandler:
  19. Close #iFileNumber
  21. End Function
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. '****************************************************************
  3. 'Define variables for strings
  4. Dim sFileText As String
  5. Dim sFrom1 As String, sTo1 As String
  6. '****************************************************************
  8. 'Open the HTML file as text
  9. sFileText = ReadFileContents("c:\toc\toc.htm")
  11. '****************************************************************
  12. 'In this section you should setup the strings you are searching
  13. 'The document for. You should also setup the replacement string
  14. 'If the above named strings are found
  15. '****************************************************************
  16. sFrom1 = "<img src=" + Chr(34) + "space.gif" + Chr(34)
  17. MsgBox sFrom1
  18. sTo1 = "<div class=" + Chr(34) + "toc1" + Chr(34) + "><img src=" + Chr(34) + "space.gif" + Chr(34)
  19. MsgBox sTo1
  21. Call ReplaceText(sFileText, sFrom1, sTo1)
  22. Call SaveTextToFile("c:\toc\toc.htm", sFileText, True)
  24. 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,
Oct 3 '06 #1
5 3187
any ideas?
Oct 4 '06 #2
1,418 Expert 1GB
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 !
Oct 5 '06 #3
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?
Oct 5 '06 #4
Just change
call ReplaceText(sFileText, sFrom1, sTo1)
sFileText = ReplaceText(sFileText, UCase(sFrom1), sTo1)

This should work for you.
Sep 25 '07 #5
8,435 Expert 8TB
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.
Sep 26 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.