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;">(</span><span style="color: #FF0000;">string</span> Url<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #0600FF;">if</span> <span style="color: #000000;">(</span>!Url.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">(</span><span style="color: #808080;">"://"</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>
Url = <span style="color: #808080;">"http://"</span> + Url;
<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;">(</span>Url<span style="color: #000000;">)</span>.<span style="color: #0000FF;">Host</span>;
<span style="color: #000000;">}</span>
<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;">(</span><span style="color: #FF0000;">string</span> Url<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #0600FF;">if</span> <span style="color: #000000;">(</span>Url.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">(</span><span style="color: #808080;">@"://"</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>
Url = Url.<span style="color: #0000FF;">Split</span><span style="color: #000000;">(</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;">[</span><span style="color: #000000;">]</span> <span style="color: #000000;">{</span> <span style="color: #808080;">"://"</span> <span style="color: #000000;">}</span>, <span style="color: #FF0000;">2</span>, StringSplitOptions.<span style="color: #0000FF;">None</span><span style="color: #000000;">)</span><span style="color: #000000;">[</span><span style="color: #FF0000;">1</span><span style="color: #000000;">]</span>;
<span style="color: #0600FF;">return</span> Url.<span style="color: #0000FF;">Split</span><span style="color: #000000;">(</span><span style="color: #808080;">'/'</span><span style="color: #000000;">)</span><span style="color: #000000;">[</span><span style="color: #FF0000;">0</span><span style="color: #000000;">]</span>;
<span style="color: #000000;">}</span>
<span style="color: #008080; font-style: italic;">/*
** Method 3 (using regular expressions -> 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;">(</span><span style="color: #FF0000;">string</span> Url<span style="color: #000000;">)</span>
<span style="color: #000000;">{</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;">(</span>
Url,
<span style="color: #808080;">@"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$"</span>,
<span style="color: #808080;">"$2"</span>
<span style="color: #000000;">)</span>;
<span style="color: #000000;">}</span>
以上是关于csharp 从URL中提取域名的主要内容,如果未能解决你的问题,请参考以下文章