473,503 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem With String Manipulation in VB6

5 New Member
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
  4.  
  5. On Error GoTo ErrorHandler
  6. Dim iFileNumber As Integer
  7. iFileNumber = FreeFile
  8.  
  9. If Overwrite Then
  10.     Open FileFullPath For Output As #iFileNumber
  11. Else
  12.     Open FileFullPath For Append As #iFileNumber
  13. End If
  14.  
  15. Print #iFileNumber, sText
  16. SaveTextToFile = True
  17.  
  18. ErrorHandler:
  19. Close #iFileNumber
  20.  
  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. '****************************************************************
  7.  
  8. 'Open the HTML file as text
  9. sFileText = ReadFileContents("c:\toc\toc.htm")
  10.  
  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
  20.  
  21. Call ReplaceText(sFileText, sFrom1, sTo1)
  22. Call SaveTextToFile("c:\toc\toc.htm", sFileText, True)
  23.  
  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,
john
Oct 3 '06 #1
5 3515
ThatVBGuy
5 New Member
any ideas?
Oct 4 '06 #2
PEB
1,418 Recognized Expert Top Contributor
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
ThatVBGuy
5 New Member
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
psnanu
3 New Member
Hi,
Just change
call ReplaceText(sFileText, sFrom1, sTo1)
to
sFileText = ReplaceText(sFileText, UCase(sFrom1), sTo1)

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

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

Similar topics

2
4427
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
4
3470
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
10
1753
by: micklee74 | last post by:
hi if i have a some lines like this a ) "here is first string" b ) "here is string2" c ) "here is string3" When i specify i only want to print the lines that contains "string" ie the first...
22
2567
by: mann_mathann | last post by:
can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.