5

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

enter image description here

thats why I use first() in my code, even though it doesn't work either.

Alexa289
  • 8,089
  • 10
  • 74
  • 178
  • How exactly is that not working? Are you getting an unexpected value? Is it crashing? Is your device exploding? What did you find out when you debugged it? At which specific point is it going wrong? What have to tried in attempting to remedy the current issue, whatever that might be? – Mike M. Mar 30 '19 at 00:35
  • @MikeM. I am sorry, I have edited my question and I have added the reason now – Alexa289 Mar 30 '19 at 00:50
  • 1
    `words.split("\\s+".toRegex()).size` – underoid Mar 30 '19 at 00:54
  • Possible duplicate of [Split space from string not working in Kotlin](https://stackoverflow.com/questions/48379981/split-space-from-string-not-working-in-kotlin) – underoid Mar 30 '19 at 00:55
  • @underoid yup thats the answer – Alexa289 Mar 30 '19 at 01:02

5 Answers5

4

The only problem you have is you are using Kotlin's split method which returns a list of String whereas Java's split method returns an array of strings. You should be accessing size property since the return of split method here is List.

  val words = s.toString().trim()
  numberOfInputWords = words.split("\\s+".toRegex()).size
  wordsCounterTextView.text = "$numberOfInputWords"

Example for kotlin's split method

vepzfe
  • 4,217
  • 5
  • 26
  • 46
Prateek Jain
  • 1,504
  • 4
  • 17
  • 27
1

So, you could count the words by the number of newlines and spaces between them. Of course consecutive newlines and consecutive spaces count as one.

There might be a space/newline at the beginning and end. You remove that with trim(). The last word won't have a space/newline now, so you need to add 1.

val string = "Hello\nWorld"
val count = Regex("""(\s+|(\r\n|\r|\n))""").findAll(string.trim()).count() + 1
println(count) // 2
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
1

This will count how many words are in a given String.

fun wordCount(str: String): Int {
    val trimmedStr = str.trim()
    return if (trimmedStr.isEmpty()) {
        0
    } else {
        trimmedStr.split("\\s+".toRegex()).size
    }
}

It also has a fix for some of the other answers which incorrectly return 1 when given an empty string ("").

Christopher Bull
  • 2,489
  • 1
  • 23
  • 27
0

replace all \n with space and split(" "); get count of splited string

in java:

int count = multilineEditText.getText().toString().replace("\n", " ").split(" ").length;
Shahriyar Aghajani
  • 431
  • 1
  • 6
  • 22
0

Here is a cool RegEx to do what you want (\s|\n)+

someString.split("(\\s|\\n)+")

Here is a great app to test RegEx : https://regexr.com/

FrV
  • 260
  • 2
  • 9