How to find the longest word in a string with Kotlin

1 Answer

0 votes
fun longestWord(s: String): String {
    return s.split("\\s+".toRegex())
        .maxByOrNull { it.length }
        ?: ""
}

fun main() {
    println(longestWord("Could you recommend a good restaurant nearby?"))
}




/*
run:

restaurant

*/

 



answered Mar 3 by avibootz
...