![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Location: glasgow
Posts: 4
Rep Power: 0
![]() |
html tag extractor problem
Hi,
As youcan probably guess im new here, im also quite new to the whole programming thing but have been beggining to learn vb.net due to the beta being free! Anyway, Im trying to make a simple application to help making html pages in notepad and im stuck with making a button to extract html tags from a rich text box, remove them from it and put them into another box. this bit of code works fine up until a certain point when it starts returning stuff that quite clearly isnt right ie. if the text in rtb1 is: <html> <head> <title>Michael's site</title> </head> <body bgcolor=black text=F0AF link=yellow alink=red vlink=F0AF> <font face="verdana"> <h1 align=center>Hello and welcome</h1> at the third click it puts "<title>m" into the second textbox i cant for the life of me work out whats going on! <code> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim sparvar Dim strlen Dim htmlstr Dim rbloc Dim lbloc Dim newvar rbloc = 0 lbloc = 0 sparvar = "" htmlstr = "" strlen = 0 htmlstr = rtb1.Text strlen = Len(htmlstr) lbloc = InStr(htmlstr, "<") rbloc = InStr(htmlstr, ">") sparvar = Mid(htmlstr, lbloc, rbloc) htmlstr = Mid(htmlstr, rbloc + 1, strlen) rtb1.Text = htmlstr newvar = rtb2.Text rtb2.Text = newvar + " " + sparvar End Sub </code> Also as a side note, are there any naming conventions for variables and objects? Thanks Mike |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
the tags in PFO are delimited by '[' and ']' not '<' and '>'. writing <code> does nothing.
Unfortunately, I don't know VB though, so I don't know how to help you with your problem. |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
First of all, specify a type when defining a variable: Dim str As String, etc. Next, you don't always need to declare a variable - you could just as easily write it like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim lbloc As Int16 Dim rbloc As Int16 lbloc = InStr(rtb1.Text, "<") rbloc = InStr(rtb1.Text, ">") rtb2.Text = rtb2.Text + " " + Mid(rtb1.Text, lbloc, rbloc) rtb1.Text = Mid(rtb1.Text, rbloc + 1, rtb1.Text.Length) End Sub |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0
![]() |
I think it's because of the third parameter of the mid-function. It should be the length of the string you want, not the position of the last character.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jun 2005
Location: glasgow
Posts: 4
Rep Power: 0
![]() |
Thanks everyone, espcially gilward, i didnt realise that and i think it should sort the problem! gonna nip back off and see if i can sort it.
ta again Mike |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Feb 2005
Location: Limbo
Posts: 39
Rep Power: 0
![]() |
If you are new to VB.NET, I would suggest starting to get those old adages concrete before you start another major project:
1) Name variables wisely. rtb1 could be txtUnstrippedHTML ; lbloc could be strLeftPosition 2) Declare variables with specific types. Dim rtb1? Better, Dim rtb1 as Integer This improves speed vastly, and helps to catch errors 3) Initialize variables with the declaration if possible. Dim rtb1 as Integer = 0 ; Dim lbloc as String = InStr(htmlstr, "<") 4) Even though it is possible, try not to declare variables on the fly. In a large application with hundreds of variables, this can save hours of debugging. Just my two cents here. Ignore if need be ![]()
__________________
The meek will inherit the earth. -WDaquell |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
bah. Maybe I'm just too used to the C/C++/other cool languages/etc. culture, but I don't like the "fooBarQuux" type variable names. I much greaterly suggest "foo_bar_quux".
|
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
In VB and C#, most people use fooBarWibble, so it's generally expected that you do too.
|
|
|
|
|
|
#9 | |
|
Expert Programmer
|
Quote:
Add an Option Explicit line and let's pretend this never happened ![]() |
|
|
|
|
|
|
#10 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Erm... read my code. By that, I meant that instead of recording things in weirdly-named variables, he could just plug them straight into the functions he was using them in.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|