How to get the first word from a string in Kotlin

1 Answer

0 votes
fun getFirstWord(s: String): String {
    return s.split(" ")[0]
}

fun main() {
    val s = "kotlin java c c++ c# python"
    
    val firstWord = getFirstWord(s)
    
    println("The first word is: $firstWord")
}


 
/*
run:
 
The first word is: kotlin
 
*/

 



answered Oct 31, 2024 by avibootz
...