Extract domain name from URL

Shows three different methods how to extract the domain name of a given web adress.

/* 
** Method 1 (using the build-in Uri-object)
*/
public static string ExtractDomainNameFromURL_Method1(string Url)
{
    if (!Url.Contains("://"))
        Url = "http://" + Url;
 
    return new Uri(Url).Host;
}
 
/*
** Method 2 (using string modifiers)
*/
public static string ExtractDomainNameFromURL_Method2(string Url)
{
    if (Url.Contains(@"://"))
        Url = Url.Split(new string[] { "://" }, 2, StringSplitOptions.None)[1];
 
    return Url.Split('/')[0];
}
 
/*
** Method 3 (using regular expressions -> slowest) 
*/
public static string ExtractDomainNameFromURL_Method3(string Url)
{
    return System.Text.RegularExpressions.Regex.Replace(
        Url, 
        @"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$", 
        "$2"
    );
}
Snippet Details



// Some example urls:
string[] Urls = new string[] {
    "http://www.jonasjohn.de/snippets/csharp/",
    "www.jonasjohn.de/snippets/csharp/",
    "http://www.jonasjohn.de/",
    "ftp://www.jonasjohn.de/",
    "www.jonasjohn.de/",
    "https://subdomain.abc.def.jonasjohn.de/test.htm"
};
 
// Test all urls with all different methods:
foreach (string Url in Urls){
    Console.WriteLine("Method 1: {0}", ExtractDomainNameFromURL_Method1(Url));
    Console.WriteLine("Method 2: {0}", ExtractDomainNameFromURL_Method2(Url));
    Console.WriteLine("Method 3: {0}", ExtractDomainNameFromURL_Method3(Url));
}

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

smith May 19, 2011 at 11:31
which one is fastest, and why is regular expression the slowest? which other method can be use to match url faster e,g domain.com or www.domain.com or
Majid May 12, 2011 at 15:07
Valuable information.. It saved my time :)
usman April 14, 2011 at 08:15
we can do it in a very simple way
Request.Url.Host
Rob October 07, 2010 at 15:55
Hi, useful solutions here - thanks a lot!

I am having a little trouble though, and I can't see why. I need to extract the domain name from a URL in an ETL program for server logs, and while I can do this succesfully with any of those methods, even the fastest is slowing my program down by about 20x. I can't really see why these would be such expensive operations, but the evidence is in front of me - if I replace the operation with a string to test, the application is MUCH quicker. Are these methods really that expensive?

Thanks
Nullstr1ng February 21, 2010 at 09:11
or this one

static string GetDomainName(string url)
{
string domain = url.Split(new string[] { "://" }, 2, StringSplitOptions.RemoveEmptyEntries)[1];
domain = domain.Split(new char[] { '/' })[0];

string[] sectons = domain.Split(new char[] { '.' });

if (sectons.Length >= 3)
{
domain = string.Join(".", sectons, sectons.Length - 2, 2);
}

return domain;
}
Nullstr1ng February 21, 2010 at 09:08
static string GetDomainName(string url)
{
string domain = url.Split(new string[] { "://" }, 2, StringSplitOptions.RemoveEmptyEntries)[1];
domain = domain.Split(new char[] { '/' })[0];

string[] sectons = domain.Split(new char[] { '.' });

if (sectons.Length == 3)
{
domain = string.Join(".", sectons, 1, 2);
}

return domain;
}
John Williams February 10, 2010 at 20:58
Method 2 does the job.
KJ January 30, 2009 at 15:23
"http://www.example.com" will return "www.example.com" that's incorrect since the domain is 'example.com' ('www' is a sub domain of 'example.com')
Brian June 17, 2008 at 15:29
Method 2 worked great for me, even when the domain was an IP address. Thank you!
Pat April 14, 2008 at 18:46
#1 has a minor bug if url is
www.yahoo.com/red.aspx?a=http://www.cool.com

then http isnt pasted before www.yahoo.com

int pos = Url.indexOf("://");
if (pos == -1 || pos > 5)
Url = "http://" + Url;

return new Uri(Url).Host;
Jonas December 11, 2007 at 19:00
On my system Method 3 works (VS Express / .NET Framework 2.0).
Andrew December 11, 2007 at 18:08
Method 3 doesnt work.