malfunctioning string function

genu

Well-known member
Joined
Jun 9, 2005
Messages
94
Programming Experience
1-3
here is the code:

VB.NET:
Function get_pname(ByVal text As String)
        Dim num As String
        Dim c
        Dim s As Integer
        Dim t
        Dim r
        For c = text.IndexOf("<SPAN class=LstName>", r) + 20 To text.Length - 1
            If text.Chars(c) = ">" Then
                MsgBox(r)
                For t = c To text.Length - 1
                    If text.Chars(t) = "<" Then
                        num = text.Substring(c + 1, t - c - 1)
                        r = (t - c - 1)
                        Return (num)
                    End If
                Next
            End If
            MsgBox(r)
        Next
    End Function
this function is supposed to search for "<SPAN class=LstName>" in a whole bunch of html code. then go right character by character until it reaches the ">". After that it puts in variable num eveerything between the > and the next < tags. which its supposed to be a person name. The problem is that there are a lot of lines like this in the html file, and I want this function to keep doing the same thing for the next lines. Thats why I added the r variable, to start the first LOOP lookking for the "<SPAN class=LstName>" at r everytime. but for some reason the result is always 10 records of each line with the same name. Something is wrong with the r. it doesnt start from r postiion in the string like its supposed to.
 
If you want help with something like this can I suggest some comments and/or descriptive variable names. It's not clear exactly what num, c, s, t and r are supposed to be, and I'm not prepared to study your code to try to work it out.

On an unrelated matter, I recommend you avoid late binding. You should always declare the type of your variables.
 
ok ill try to put together a better code...sorry...it was late at night and I was stressed out...lol....
 
ok in a nutshell here is what I want to do:

I have this html CODE:

HTML:
<TD valign=top><SPAN class=LstName><A name="&lpos=Results&lid=Persons+Name" HREF="something">william, fuler</A>
<TD valign=top><SPAN class=LstName><A name="&lpos=Results&lid=Persons+Name" HREF="something">Wilson, Charles</A>
<TD valign=top><SPAN class=LstName><A name="&lpos=Results&lid=Persons+Name" HREF="something">bob, saget</A>
My code is basically supposed to extract the people's names from that big string using those different string operations.
 
This isn't a solution to your problem necessarily but assuming you can load those lines into a variable and only those lines from your HTML...maybe this will help or give you a general idea of what to do:

VB.NET:
Dim marker As Integer
Dim name As String 'This is where you'd load the string to be parsed

If name.EndsWith("</A>") = True Then
	name = name.Substring(1, name.Length - "</A>".Length)
	marker = name.LastIndexOf(">")
	If marker > 0 Then
		marker += 1
		name = name.Substring(marker, name.Length - marker)
		MsgBox(name)
	End If
End If

But, yeah, get into the habit of giving all of your variables descriptive names, and always declare the type when possible. That is the single most important thing you can do to make your code readable. I saw this thread before and thought about replying but I didn't have any idea what you were trying to accomplish and I couldn't get any more of an idea from the code you pasted. You're not a sloppy coder, that's all good, just need to make it more readable. It'll pay off when you look at code you wrote 2 years after you wrote it.

Many .NET programmers use the Pascal (I believe it's Pascal?) capitalization conventions. Such as copyFiles, customerName, customerPhoneNumber. Although I usually don't allow my variable names get as long as that last example.
 
how do I get a line number from the html file. For example if I find a string in a html file, and I want to put the whole line where it found the string into a variable. so I can pass it down to another function.
 
I have ALL the html in a variable called html I want to read line by line from that variable...so...how would I make a loop to read each line?
 
Split it into an array:
VB.NET:
Dim lines As String() = System.Text.RegularExpressions[b].Regex[/b].Split(myHTMLString, Environment.NewLine)
That will remove all line breaks and make each line an element of the array. You cann then use a loop to iterate over the array line by line, so the loop counter tells you what line you are at.

Edit:
Note the edit in bold. Missed that bit in the original post.
 
jmcilhinney said:
Split it into an array

Exactly what I was just thinking! Unless you switch to reading the html file line-by-line, this is a good suggestion. Then just For loop through the array bounds, parsing out all the unwanted html junk.
 
Back
Top