加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – 获得域名扩展的最优雅方式是什么?

发布时间:2020-12-17 07:13:41 所属栏目:百科 来源:网络整理
导读:例如 google.com - .comgoogle.co.id - .co.idhello.google.co.id - .co.id 在vb.net中? 甚至可以这样做吗? 解决方法 通过假设具有各种“.”的域.必须包括“.co”.位,你可以使用这段代码: Dim input As String = "hello.google.co.id"Dim extension As St
例如

google.com -> .com

google.co.id -> .co.id

hello.google.co.id -> .co.id

在vb.net中?

甚至可以这样做吗?

解决方法

通过假设具有各种“.”的域.必须包括“.co”.位,你可以使用这段代码:

Dim input As String = "hello.google.co.id"
Dim extension As String = ""
If (input.ToLower.Contains(".co.")) Then
    extension = input.Substring(input.ToLower.IndexOf(".co."),input.Length - input.ToLower.IndexOf(".co."))
Else
    extension = System.IO.Path.GetExtension(input)
End If

UPDATE

正如评论所建议的那样,上面的代码并没有考虑到很多可能性(例如.ca.us).下面的版本来自不同的假设(.xx.yy只有在有2个字符的组时才能出现)应该处理所有可能的替代方案:

If (input.ToLower.Length > 4 AndAlso input.ToLower.Substring(0,4) = "www.") Then input = input.Substring(4,input.Length - 4) 'Removing the starting www.  

Dim temp() As String = input.Split(".")

If (temp.Count > 2) Then
    If (temp(temp.Count - 1).Length = 2 AndAlso temp(temp.Count - 2).Length = 2) Then
        'co.co or ca.ca,etc.
        extension = input.Substring(input.ToLower.LastIndexOf(".") - 3,input.Length - (input.ToLower.LastIndexOf(".") - 3))
    Else
        extension = System.IO.Path.GetExtension(input)
    End If
Else
    extension = System.IO.Path.GetExtension(input)
End If

在任何情况下,这都是一个诡辩的现实,因此这个代码(基于对情况的非常有限的理解,我目前的理解)不能被认为是100%可靠的.有些情况甚至在不知道给定字符集是否为扩展名的情况下甚至无法识别;例如:“hello.ue.co”.在某些情况下,该分析应补充有检查给定扩展是否有效的功能(例如,包括一组有效但不明显的扩展的字典),至少在某些情况下.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读