How to check the number of words in a String? [duplicate]

-1

Well, I'm doing a job for college where I need to identify the number of words a string has in Kotlin.

Type:

texto = "Ola Mundo"

The variable numeropalavra s must be equal to 2.

Is there a function that checks the number of words, without having to cycle to check the letter to find the number of words?

    
asked by anonymous 23.11.2018 / 16:02

1 answer

0

You can separate the string by the spaces between the words using the split() function and see the length (length) of the array.

val string = "Uma string de exemplo"
val numeropalavras = string.split().length
    
23.11.2018 / 16:12