Sponsored Ad

Friday, March 26, 2010

How to Get Domain Name from a URL | Extract Domain Name in C#

This utility illustrate you to get domain name from a given URL or Website page location.

The input string for this utility is URL and it returns the exact domain in string format.

 

public static string GetDomain(string url)
  {
      int pos = url.LastIndexOf("://");

      if (pos != -1)
          url = url.Substring(pos + 3);

      pos = url.IndexOf('/');

      if (pos == -1)
          return url;

      return url.Substring(0, pos);

  }

How to call this utility:

  To call this utility just call  from anywhere in code and result will return in a string.

GetDomain(http://csharptalk.com/);


Like i am calling in page_Load event of page.

protected void Page_Load(object sender, EventArgs e)
  {

      Label1.Text = GetDomain("http://csharptalk.com/");
  }

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers