What's an easy way to do this?

  • Thread starter Thread starter vbMark
  • Start date Start date
Don't know about a class that does this for you, but some string
manipulation would get you there.

Dim wholeURL As String = "http://www.microsoft.com/"

Dim baseURL As String = wholeURL.Substring(wholeURL.IndexOf("//") + 2,
wholeURL.IndexOf("/", 7) - 7)

baseURL = Right(baseURL, baseURL.Length - baseURL.IndexOf(".") - 1)

Response.Write(baseURL)
 
That's very close but I just want the microsoft.com part. I don't see a
method for that. Am I just missing it?
 
That wouldn't strip off information after the domain name though.
 
AFAIK, I don't think there is a method for that. You'd just have to
manipulate the string, splitting it at periods "." or something.
 
AFAIK, I don't think there is a method for that. You'd just have to
manipulate the string, splitting it at periods "." or something.
 
AFAIK, I don't think there is a method for that. You'd just have to
manipulate the string, splitting it at periods "." or something.

SORRY if I double/triple posted. My news server is saying that my message
is deleted right after I post it. Wtf.
 
Here is my solution:

Dim myUri As New Uri(sURI)
sTemp = myUri.Host
sSplit = sTemp.Split(".")
sTemp = sSplit(sSplit.GetUpperBound(0) - 1) & _
"." & sSplit(sSplit.GetUpperBound(0))
MsgBox(sTemp)

Let me know what you think.
 
Scott,
Two "problems" :-)

1) I would avoid mixing VB.Strings (Right) with String (IndexOf, Length) I
would recommend using one or the other, then you avoid any potential off by
one problems (do I need to include -1 or don't I). As Right expects a base 1
index, while IndexOf returns a base 0 index.

2) what happens with urls such as:

http://lab.msdn.microsoft.com/vs2005/

Hope this helps
Jay
 
I don't have a problem using "Right" and "IndexOf". They are both valid and
both have a purpose.

You are correct, though, about the longer domain name, so here is a solution
that works for all:

Dim myURI As New Uri("http://lab.msdn.microsoft.com/vs2005/")
Dim strURI As String = myURI.Host.ToString
Dim urlPart As String() = strURI.Split(CType(".", Char))
strURI = CType(urlPart(urlPart.Length - 2), String) & "." &
CType(urlPart(urlPart.Length - 1), String)
 

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