[英]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.我想要的是只返回
merriam-webster.com
我打算使用.Replace()
但我认为这个问题有更好的方法。
If you are working for Winforms then如果您正在为 Winforms 工作,那么
string url = "http://www.merriam-webster.com/dictionary/sample";
UriBuilder ub = new UriBuilder(url);
MessageBox.Show(ub.Host.Replace("www.",""));
and for web,对于网络,
How about this这个怎么样
System.Uri uri = new Uri("http://stackoverflow.com/search?q=something");
string uriHost = uri.Host;
? ?
Answers here don't handle the case like " http://abcwww.com ".这里的答案不处理像“ http://abcwww.com ”这样的情况。 Check if the url starts with 'www.'
检查网址是否以“www”开头。 before replacing it.
在更换之前。
if (url.StartsWith("www.", StringComparison.OrdinalIgnoreCase))
url = url.Replace("www.", string.Empty);
You can use this:你可以使用这个:
var a = "http://www.merriam-webster.com/dictionary/sample";
var b = a.Split('.');
var c = b[1] +"."+ b[2].Remove(3);
You can leverage System.Uri
class:您可以利用
System.Uri
类:
System.Uri uri = new Uri("https://www.google.com/search?q=something");
string uriWithoutScheme = uri.Host + uri.PathAndQuery + uri.Fragment;
Or you can use below:或者你可以在下面使用:
UriBuilder ub = new UriBuilder("https://www.google.com/search?q=something");
currentValue = ub.Host.Replace("www.", "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.