How to get string within string

  • Thread starter Thread starter Richard Aubin
  • Start date Start date
R

Richard Aubin

I have this code in html:

<a href="domain.com">click here</a>

How can I grab the string click here or any other strings between "> and
</a>?
 
August 11, 2004

You would have to add an "id" property to it. Then you can reference
it
in code.

<a href="domain.com" id="myahref">click here</a>

Then you can use the Controls collection to get the string...

Private Sub Button1_Click(...)
label1.text = myahref.Controls(0)
End Sub

If you have controls within it:

<a href="domain.com" id="myahref">click here
<asp:Textbox id="mytextbox" ... />
</a>

Then you would have to know what index the string is at in the Controls
Collection. I hope this answers your question!


Joseph MCP
 
Hello Joseph,

I think what I want to do is the following.

Find the first occurence of
..com">

Count 6 characters to start the index after the >
Find the position of </a> in the string (-1 to go to the character before
the "<")

Then grab everything between Starting Position and the second position.

I'm doing this to a remote html page that I don't have access to.
 
Richard Aubin said:
Hello Joseph,

I think what I want to do is the following.

Find the first occurence of
.com">

What about if it's not a .com address (such as .edu)? I'd get the position
of the first ">" and the last "<" and grab everything in between.
 
Richard said:
I have this code in html:

<a href="domain.com">click here</a>

How can I grab the string click here or any other strings between ">
and </a>?

How's this?

\\\
Dim s As String = "<a href=""domain.com"">click here</a>"
Dim startPos As Integer
Dim endPos As Integer
Dim t As String

'Get the position of the > character
startPos = InStr(s, ">")
'Get the position of the </a> characters
endPos = InStr(s, "</a>")
'Make sure both strings were found and the end pos is after the startpos
If startPos > 0 And endPos > startPos Then
'Skip over the > character itself
startPos += 1
'Extract the characters between startPos and endPos
t = Mid(s, startPos, endPos - startPos)
'Display the result
MsgBox(t)
End If
///
 
You need the InStr command:
i = Instr(myStr, ".com>"
newStr = mid(myStr, i + 5)
 
This is exactly what I needed. Thanks!

Works like a charm!

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top