To iterate over words in a string in Kotlin, you can use the split
function with a space as the delimiter.
In this example,
str
with a value of "Hello World Kotlin"
.split(' ')
function on the string to split it into words based on spaces.fun main() {
val str = "Hello World Kotlin"
val words = str.split(' ')
for (word in words) {
println(word)
}
}
Hello World Kotlin
In this tutorial, we learned How to Iterate over Words in String in Kotlin language with well detailed examples.