I have writtent following code to check if string contains any of of the words.
fun String.isValidUrl(): Boolean {
return contains("http://") || contains("https://") || contains("www.")
}
How can I simplify this statement in kotlin.
In this case, you may want to use startsWith() rather than contains(). Or it might not detect invalid URLs like, say, abc://www.example.com
You can use this:
val prefixes = arrayOf("http://", "https://", "www.")
fun String.isValidUrl() = prefixes.any { this.startsWith(it) }
OR
Use Android's built-in URLUtil.isValidUrl()