I find it hard to write good Swift code, aka, that reads easy and is not long to do this:
I have a domain name, that could potentially hold one or more subdomains, so not just "irc.libera.chat" but it could be also "a.b.c.whatever.com".
What I want to do is extract the domain, so that means only the word before and after the last dot, with the dot itself included "libera.chat" and "whatever.com"
Another alternative, which wouldn't require you to reconstruct the string by adding the dot back, would be to find the index of the second to last word, and use that to grab the substring. It also gives you nice place to handle the errors
let domain = "irc.libera.chat"
guard let idx = domain.split(separator: ".").dropLast().last?.startIndex else {
// handle strings without any dots
}
let substring = domain[idx...]