![]() |
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 |
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. |
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 |
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.
|
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 |
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 :) |
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".
|
In VB and C#, most people use fooBarWibble, so it's generally expected that you do too.
|
Quote:
Add an Option Explicit line and let's pretend this never happened :) |
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.
|
| All times are GMT -5. The time now is 2:02 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC