티스토리 뷰

공부

[kotlin] URL.domain()

승가비 2022. 6. 20. 00:36
728x90

https://stackoverflow.com/questions/9607903/get-domain-name-from-given-url/72678203#72678203

 

Get domain name from given url

Given a URL, I want to extract domain name(It should not include 'www' part). Url can contain http/https. Here is the java code that I wrote. Though It seems to work fine, is there any better appro...

stackoverflow.com

const val WWW = "www."

fun URL.domain(): String {
    val domain: String = this.host
    return if (domain.startsWith(ConstUtils.WWW)) {
        domain.substring(ConstUtils.WWW.length)
    } else {
        domain
    }
}
728x90
댓글