簡體   English   中英

如何在C#中刪除URL中的www、http/https等字符串

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

例如我有這個字符串

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

我想要的是只返回merriam-webster.com我打算使用.Replace()但我認為這個問題有更好的方法。

如果您正在為 Winforms 工作,那么

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

UriBuilder ub = new UriBuilder(url);

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

對於網絡,

從 URL 獲取主機域?

這個怎么樣

System.Uri uri = new Uri("http://stackoverflow.com/search?q=something");
string uriHost = uri.Host;

?

這里的答案不處理像“ http://abcwww.com ”這樣的情況。 檢查網址是否以“www”開頭。 在更換之前。

if (url.StartsWith("www.", StringComparison.OrdinalIgnoreCase))
    url = url.Replace("www.", string.Empty);

你可以使用這個:

        var a = "http://www.merriam-webster.com/dictionary/sample";
        var b = a.Split('.');
        var c = b[1] +"."+ b[2].Remove(3);

您可以利用System.Uri類:

System.Uri uri = new Uri("https://www.google.com/search?q=something");
string uriWithoutScheme = uri.Host + uri.PathAndQuery + uri.Fragment;

或者你可以在下面使用:

UriBuilder ub = new UriBuilder("https://www.google.com/search?q=something");
currentValue = ub.Host.Replace("www.", "");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM