google.com -> .com
google.co.id -> .co.id
hello.google.co.id -> .co.id
在vb.net
?
即使這樣做可以嗎?
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」。這種分析應該補充一個函數,檢查給定的擴展是否有效(例如,字典包括一組有效但不明顯的擴展),至少在某些情況下。
這可能適用於OP,但請記住,在那裏這是很多例外。例如'.ca.us'或'.ny.us'。還有像'x.co'這樣的地址。見:http://en.wikipedia.org/wiki/.co – Gray
@格雷謝謝你讓我知道。不知道說實話;這就是爲什麼我把我的假設寫在頂端;現在將使用此信息更新我的答案。 – varocarbas
@Gray更新後的版本負責處理任何xx.yy案件,並且完全不依賴於.co。如果知道進一步的情況仍然沒有被這個代碼說明,請告訴我。 – varocarbas
你不能這樣做自動神奇。如果可行的話,我會使用來自良好來源的頂級域名列表,甚至使用圖書館。你不能用類似正則表達式的東西來做,因爲有時候你想要最後一個點後面的字符串,但有時候你想要最後兩個。 – Gray
你可以使用System.IO.FileInfo獲得.com的擴展名,但是它會如何知道google是域名,並且你希望在此之後的所有內容。 – Steve
有沒有特殊的vb.net功能? –