AJB AJB - 2 months ago 21
C# Question

How to remove www, http/https and other strings in URL in C#

For Example I have this string

http://www.merriam-webster.com/dictionary/sample


What I want is to return only
merriam-webster.com
I'm planning to use
.Replace()
but I think there are better approach for this question.

Answer

If you are working for Winforms then

string url = "http://www.merriam-webster.com/dictionary/sample";

UriBuilder ub = new UriBuilder(url);

MessageBox.Show(ub.Host.Replace("www.",""));

and for web,

Get host domain from URL?