csharp 从URL中提取域名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 从URL中提取域名相关的知识,希望对你有一定的参考价值。

<span style="color: #008080; font-style: italic;">/* 
** Method 1 (using the build-in Uri-object)
*/</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> ExtractDomainNameFromURL_Method1<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> Url<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>!Url.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;://&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        Url = <span style="color: #808080;">&quot;http://&quot;</span> + Url;
&nbsp;
    <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Uri<span style="color: #000000;">&#40;</span>Url<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Host</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/*
** Method 2 (using string modifiers)
*/</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> ExtractDomainNameFromURL_Method2<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> Url<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Url.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">@"://&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        Url = Url.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #808080;">&quot;://&quot;</span> <span style="color: #000000;">&#125;</span>, <span style="color: #FF0000;">2</span>, StringSplitOptions.<span style="color: #0000FF;">None</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>;
&nbsp;
    <span style="color: #0600FF;">return</span> Url.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">'/'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/*
** Method 3 (using regular expressions -&gt; slowest) 
*/</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> ExtractDomainNameFromURL_Method3<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> Url<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">return</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Text</span>.<span style="color: #0000FF;">RegularExpressions</span>.<span style="color: #0000FF;">Regex</span>.<span style="color: #0000FF;">Replace</span><span style="color: #000000;">&#40;</span>
        Url, 
        <span style="color: #808080;">@"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$&quot;</span>, 
        <span style="color: #808080;">&quot;$2&quot;</span>
    <span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>

以上是关于csharp 从URL中提取域名的主要内容,如果未能解决你的问题,请参考以下文章

如何从 URL 中提取***域名 (TLD)

从给定的 url 中提取主域名

在 Python 中从 URL 中提取域名

如何从网址中仅提取域名? [复制]

从 URL 获取域名/主机名的最快方法是啥?

从给定的url获取域名