.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Get Domain Name

Posted By:Sasi Prabhu       Posted Date: July 30, 2011    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Way to Extract the Domain Name the URL.

Using Inbuild .Uri Object - Host:

public static string ExtractDomainNameFromURL(string Url)

    if (!Url.Contains("://"))

        Url = "http://" + Url;
    return new Uri(Url).Host;


Using String Modifiers - Split:

public static string ExtractDomainName(string Url)

 if (Url.Contains(@"://")) Url = Url.Split(new string[] { "://" }, 2, StringSplitOptions.None)[1]; return Url.Split('/')[0]; }

Using Regular Expression :

public static string ExtractDomainName(string Url)
    return System.Text.RegularExpressions.Regex.Replace(Url,@"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$", "$2");


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend