I have tried to read this thread: Android - java - count words
but it doesn't work for me.
so let say I have these words in the android multiline edit text:
I
am
very very
happy
right now
so I want to count the number of words and then get integer '7' from that multiline edit text. how to do that ?
I have tried this, but it doesn't work:
multilineEditText.addTextChangedListener(object: TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
val words = s.toString().trim()
numberOfInputWords = words.split("\\s+").first().length
wordsCounterTextView.text = "$numberOfInputWords"
}
})
but this code doesn't work for me, because it doesn't show the right number.
from the thread Android - java - count words
it is said that I can use someString.split("\\s+").length
but I can't access .length
after using .split("\\s+")
. like this
thats why I use first()
in my code, even though it doesn't work either.