For example
google.com -> .com
google.co.id -> .co.id
hello.google.co.id -> .co.id
in vb.net?
Can that even be done?
For example
google.com -> .com
google.co.id -> .co.id
hello.google.co.id -> .co.id
in vb.net?
Can that even be done?
By assuming that domains with various "." have to include the ".co." bit, you can use this code:
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
As suggested via comments, the code above does not account for quite a few eventualities (e.g., .ca.us). The version below comes from a different assumption (.xx.yy can be present only if there are groups of 2 characters) which should take care of all the potential alternatives:
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
In any case, this is a casuistic reality and thus this code (built on a pretty limited understanding of the situation, my current understanding) cannot be considered 100% reliable. There are cases which cannot even be identified without knowing if the given set of characters is an extension or not; for example: "hello.ue.co". This analysis should be complemented with a function checking whether the given extension is valid or not (e.g., dictionary including a set of valid, although not evident, extensions), at least, in certain cases.