0

Possible Duplicate:
c#: Get just the domain name from a URL?
C# : Getting exact domain name from any URL

I am trying to remove the top level domains extension from a given URL.

Example www.google.com, www.google.co.uk

I can extract the host and using Uri class, and can strip away the "www.".

I can also strip the .com, .org or any other extension as long as it is 3 characters.

How would I go about removing extension such as .co.uk or any URL that contains something different than the standard.

I really just need to extract the Top level domain ex ("google" or "maps.google").

Any help on this would be greatly appreciated.

Here is a code snippet of how I extract the last item of Uri object.

public static string Test(Uri uri)
    {
        if (!uri.HostNameType.Equals(UriHostNameType.Dns) || uri.IsLoopback)
            return string.Empty; // or throw an exception
        return uri.Host.Split('.').Last();
    }
Community
  • 1
  • 1
Adam N
  • 115
  • 1
  • 11
  • There is no generic solution to this problem. For example, for `www.example.co.uk` the result is `example`, but for `www.example.co.de` the result is `co`. – dtb Oct 25 '12 at 00:39
  • first, a point of terminology; the TLD is the rightmost part of the URL (.com, .edu, .org, etc). also note that by a strict definition, your code already works fine, since the .co is not part of the TLD for .co.uk . you can use a webservice like this: http://domainapi.com/documentation/how-to-use-domainapi/servuces-provided/top-level-domain-knowledge-base-api.html to retrieve a list of TLDs, but they won;t help with your specific problem in the case of .co.uk or .co.de . – Frank Thomas Oct 25 '12 at 01:57

0 Answers0