Haskell
Note: Assuming the string will always start with either http:// or https:// or nothing but not supporting any other protocols (by lazyness).
http://
https://
Note2: This won't work for the URIs that have a dot in their name, still working on it.
Note3: Working all good but the end is a mess haha...
import Control.Arrow import Data.List (isPrefixOf) removeProtocol :: String -> String removeProtocol "" = "" removeProtocol string@(firstCharacter : rest) | isPrefixOf https string = removeProtocol (drop (length https) string) | isPrefixOf http string = removeProtocol (drop (length http) string) | otherwise = firstCharacter : removeProtocol rest where https = "https://" http = "http://" countDots :: String -> Int countDots = filter (== '.') >>> length dropUntilWithDot :: String -> String dropUntilWithDot = dropWhile (/= '.') >>> drop 1 domainName :: String -> String domainName url | countDots url == 1 = takeWhile (/= '.') urlWithoutProtocol | otherwise = takeWhile (/= '.') $ iterate dropUntilWithDot url !! (countDots (takeWhile (/= '/') urlWithoutProtocol) - 1) where urlWithoutProtocol = removeProtocol url main :: IO () main = do print $ domainName "domain.com" -- domain print $ domainName "http://domain.com" -- domain print $ domainName "https://domain.com" -- domain print $ domainName "api.domain.com" -- domain print $ domainName "http://api.domain.com" -- domain print $ domainName "https://api.domain.com" -- domain print $ domainName "dev.api.domain.com" -- domain print $ domainName "http://dev.api.domain.com" -- domain print $ domainName "https://dev.api.domain.com" -- domain print $ domainName "https://dev.api.domain.com/something.cool" -- domain
Try it online
Hosted on Repl.it.
Have you tried it using a .co.uk TLD? :D
.co.uk
No I didn't and I assume it will fail for this particular case. Do you have any way on improving this one?
Unfortunately, I think the only way to improve on it is to use the list of all TLDs to find how much of the end of the domain is TLD.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Haskell
Note: Assuming the string will always start with either
http://
orhttps://
or nothing but not supporting any other protocols (by lazyness).Note2: This won't work for the URIs that have a dot in their name, still working on it.
Note3: Working all good but the end is a mess haha...
Try it online
Hosted on Repl.it.
Have you tried it using a
.co.uk
TLD? :DNo I didn't and I assume it will fail for this particular case. Do you have any way on improving this one?
Unfortunately, I think the only way to improve on it is to use the list of all TLDs to find how much of the end of the domain is TLD.