How to get the length of the last word in a string with Kotlin

1 Answer

0 votes
fun main() {
    val str = "c++ c php javascript rust kotlin"
    
    val words = str.split(" ")
    val lastWord = words.last()
    
    println("The length of the last word is: ${lastWord.length}")
}

 
  
/*
run:
  
The length of the last word is: 6

*/

 



answered Jun 25, 2025 by avibootz
...