Getting the end of a domain name

A

Adam Tibi

Hello,

I want to get the right most name of the a domain name, for example:

if the domain is www.myweb.com , I want to get myweb.com
something.myweb.com --> myweb.com
www.myweb.com.au --> myweb.com.au
something.myweb.com.au --> myweb.com.au
www.myweb.tv --> myweb.tv
www.something.myweb.net --> myweb.net
my.test.myweb.ca --> myweb.ca

I want to know if there is a general rule, code or article regarding this
issue.
I tried the following code, but I am not convinced, because it is not
generic
public static string GetDomainName(string serverName ) {

if(!serverName.Contains(".")) {

return serverName.ToLower();

}

if(serverName.EndsWith(".co.uk")) {

return serverName.Substring(serverName.LastIndexOf(".", serverName.Length -
".co.uk".Length - 1) + 1).ToLower();

}

if(serverName.EndsWith(".com") || serverName.EndsWith(".net") ||
serverName.EndsWith(".org")) {

return serverName.Substring(serverName.LastIndexOf(".", serverName.Length -
5) + 1).ToLower();

}

return null;

}

Does any one have a better solution?
Adam Tibi
 
G

Guest

Adam,
I'd take a look at the System.Uri class, which has some built-in properties
that simplify segmenting out the portions of a Uri. There may be additional
ways to do it that I'm not aware of.
Peter
 
A

Adam Tibi

Thank you for the reply Peter, however, I did check it but I couldn't find
this specific requirment

Regards
 
V

vinu

Hi,

Save your primary domain name (myweb) globally. Get the host name with HTTP_HOST server variable and use substring function

e.g.

string tct = @"www.something.myweb.net.au";
MessageBox.Show(tct.Substring(tct.IndexOf("myweb")));

Will display out as myweb.net.au

Hope this might help you...



Vinu
 
A

Adam Tibi

Thank you Vinu,

The problem here is that I don't know the domain name in advance, I am going to get different domain names and I need to parse them, so this solution doesn't work because it assumes that I know the domain name in advance. I want a global solution that will get the last portion of any domain name.

ideas any one?
Regards
Adam Tibi
Hi,

Save your primary domain name (myweb) globally. Get the host name with HTTP_HOST server variable and use substring function

e.g.

string tct = @"www.something.myweb.net.au";
MessageBox.Show(tct.Substring(tct.IndexOf("myweb")));

Will display out as myweb.net.au

Hope this might help you...



Vinu
 
C

clintonG

As Peter tried to convey, there's probably a method in a class the framework
provides. The "long hand" way I did this in ASP can still be done using
ASP.NET...

* Get the URL as a string and trim white space from front and back
* Pass the trimmed string to a string reverse method
* Split the reversed string on the . character
* Get the value from the [0]th array indice
* Reverse that value and you get "com", "org", "aero", "name" or whatever

Note when we split a string an array is created. Simply get the value from
the [0]th indice as I indicate and reverse the value and you get what you
want.


<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/


Thank you Vinu,

The problem here is that I don't know the domain name in advance, I am going
to get different domain names and I need to parse them, so this solution
doesn't work because it assumes that I know the domain name in advance. I
want a global solution that will get the last portion of any domain name.

ideas any one?
Regards
Adam Tibi
Hi,

Save your primary domain name (myweb) globally. Get the host name with
HTTP_HOST server variable and use substring function

e.g.

string tct = @"www.something.myweb.net.au";
MessageBox.Show(tct.Substring(tct.IndexOf("myweb")));
Will display out as myweb.net.au
Hope this might help you...

Vinu
 
A

Adam Tibi

Thank you Clinton,

You are assuming that the extention is just one part like "org" or "net" but
it might be "com.au" or "co.uk" and I currently made a solution for the
extentions that I know but I wanted a generic solution that doesn't require
me to check .com.au, .com.lb, .co.uk, ca, net, org, us, info, etc...

I just wanted a general rule for this.

Regards,
Adam Tibi

clintonG said:
As Peter tried to convey, there's probably a method in a class the
framework provides. The "long hand" way I did this in ASP can still be
done using ASP.NET...

* Get the URL as a string and trim white space from front and back
* Pass the trimmed string to a string reverse method
* Split the reversed string on the . character
* Get the value from the [0]th array indice
* Reverse that value and you get "com", "org", "aero", "name" or whatever

Note when we split a string an array is created. Simply get the value from
the [0]th indice as I indicate and reverse the value and you get what you
want.


<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/


Thank you Vinu,

The problem here is that I don't know the domain name in advance, I am
going to get different domain names and I need to parse them, so this
solution doesn't work because it assumes that I know the domain name in
advance. I want a global solution that will get the last portion of any
domain name.

ideas any one?
Regards
Adam Tibi
Hi,

Save your primary domain name (myweb) globally. Get the host name with
HTTP_HOST server variable and use substring function

e.g.

string tct = @"www.something.myweb.net.au";
MessageBox.Show(tct.Substring(tct.IndexOf("myweb")));
Will display out as myweb.net.au
Hope this might help you...

Vinu



Adam Tibi said:
Hello,

I want to get the right most name of the a domain name, for example:

if the domain is www.myweb.com , I want to get myweb.com
something.myweb.com --> myweb.com
www.myweb.com.au --> myweb.com.au
something.myweb.com.au --> myweb.com.au
www.myweb.tv --> myweb.tv
www.something.myweb.net --> myweb.net
my.test.myweb.ca --> myweb.ca

I want to know if there is a general rule, code or article regarding this
issue.
I tried the following code, but I am not convinced, because it is not
generic
public static string GetDomainName(string serverName ) {

if(!serverName.Contains(".")) {

return serverName.ToLower();

}

if(serverName.EndsWith(".co.uk")) {

return serverName.Substring(serverName.LastIndexOf(".",
serverName.Length -
".co.uk".Length - 1) + 1).ToLower();

}

if(serverName.EndsWith(".com") || serverName.EndsWith(".net") ||
serverName.EndsWith(".org")) {

return serverName.Substring(serverName.LastIndexOf(".",
serverName.Length -
5) + 1).ToLower();

}

return null;

}

Does any one have a better solution?
Adam Tibi
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top