1

I use the following code to remove http:// and www. or dev. from a URL:

Uri uri = new Uri(this.Referrer);
    if (uri != null )
        return uri.GetLeftPart(UriPartial.Authority).Replace("http://dev.", "").Replace("http://www.", "").Replace("http://", "");
    else
        return null;

I don't like that I'm relying on the .Replace() function. I had a bug for quite a while until I realized that the this.Referrer didn't have the subdomain.

Is there a more elegant way to do this?

EricLaw
  • 56,563
  • 7
  • 151
  • 196
Jon Harding
  • 4,928
  • 13
  • 51
  • 96
  • Similar question: http://stackoverflow.com/questions/4643227/top-level-domain-from-url-in-c-sharp – user2737037 Sep 05 '13 at 19:11
  • 1
    It's similar but not really applicable. – Mark Walsh Sep 05 '13 at 19:26
  • Surely the question is also not well phrased as even so called top level domains are all still sub-domains of the root domain `.(empty string)`. Doing it reliably would require having a list of the top level domains and then just taking one domain level below that. – Klors Sep 05 '13 at 19:26
  • Check out this answer: http://stackoverflow.com/a/4983072/188246 (You will have to change the pastebin a bit to work for you) – David Sherret Sep 05 '13 at 19:36
  • 1
    A hardcoded list...I'd advise against this, it's a big code smell and unnecessarily bloated. – Mark Walsh Sep 05 '13 at 19:45
  • How do you tell if something is a domain and not a tld then? For example, any `co.uk` tld. – David Sherret Sep 05 '13 at 21:03
  • I've updated my regex to be less greedy. – Mark Walsh Sep 05 '13 at 21:27
  • 3
    It doesn't have to be a hard coded list, feel free to fetch the top level domains from the IANA - http://data.iana.org/TLD/tlds-alpha-by-domain.txt – Klors Sep 05 '13 at 21:29
  • Still think it may be a little overkill considering what's been asked. If OP was asking about URLs with many sub domains, then possibly regex wouldn't be a solution. – Mark Walsh Sep 05 '13 at 21:32
  • The only things I personally need to account for are `http://www.`, `http://` and `http://dev.` – Jon Harding Sep 05 '13 at 21:49
  • Great! In that case we should act on the YAGNI principle! – Mark Walsh Sep 06 '13 at 10:24

1 Answers1

1

You could try using a regex like this:

http:\/\/(.*?)[.?]|http:\/\/

Instead of performing multiple replaces. This would catch any other sub-domains you encounter. I'm not aware of another way you can achieve this.

This is actually not as short as it could be but I wanted to keep it readable.

Mark Walsh
  • 3,241
  • 1
  • 24
  • 46