Counting Words - Help

Hello.

I promised myself I wouldn’t post such a simple question, but this has been driving me nuts.

I am trying to count words in a TextArea and keep a running count as words are added/deleted.

If I grab the TextArea contents, split using " " as a delimiter and count the results, that’s been my most successful effort so far. But, it’s not accurate. For example, if the content is: “There are four words.” then the count comes up one word short because there’s no trailing space.

Better would be using a regular expression, but nothing I have tried in that regard has worked. I have tried examples from other threads and other sites all to no avail. I have had times where it counted “sample text” as one word then dropped to zero words when I added a third word.

I am using 2018 R3, so my first question is whether anything has changed since then that might affect the outcomes. I don’t get errors so I’m guessing there’s not an issue there but still something doesn’t make sense.

For example, I tried this suggested code in this post:

That post was from 2018 so I would think it should probably work in 2018 R3 but when I tried it, it counted “Sample text” as 0 or 1 word and “This is four words.” as 0 or 1 word.

And it’s all just driving me nuts. I can’t seem to make heads or tails out of why anything I use for a regular expression generates a result of 0 or 1 word. And, especially, “w+” which makes the app crash when I type a word into the text area.

Thanks!

@Dan_C_Rinnert,

Dan, take a look at this post splitting a string by a couple different delimiters as well. Both Kem and Tim contributed working solutions. Tim’s perhaps the more approachable in your context of not having to troubleshoot regex. Both effective.

Kind regards, Andrew

1 Like

Use the trim() function to remove any leading or trailing spaces from the text field contents, and then use the split or countfields() function, then add one to the result.

Edit: I haven’t used countfields() in a while, but I don’t think you would have to add one to the result if you use it.

That should work just fine. You get an array with a Ubound of 3, but since it is zero-based, the Count is one more, ie., 4. Make sure you Trim the string before you split so you don’t get a bogus blank entry from, say, "There are four words. "

I’d split the text by EndOfLine, trim each string, and count the spaces with CountFields.

a = Textarea1.Text.Trim

Var p() As String = a.Split(EndOfLine)
Var k As Integer = p.LastIndex

Var n As Integer

For i As Integer = 0 To k
  Var t As String = p(i).Trim
  n = n + t.CountFields(" ")
Next

MessageBox("There are " + n.tostring + " words")

I pasted a bunch of text in Scrivener, and then the same text into the textarea. Scrivener and this code gave me the same result of 3,611 words.

You may want to use a regex to remove double (or more) spaces, and double (or more) EndOfLines.

Thanks all!

@Mark_Sweeney’s example, after some reworking to work with 2018R3, worked well.

I still need to add a RegEx to compensate for multiple spaces and lines, but I’ll come back and do that another day while I work on other parts of the program. I’m kind of tired of working on this portion for now.

Also, looks like it might be time to upgrade. Seems like there have been a lot of changes from 2018 R3 to now and perhaps that is why I have had problems getting other examples to work.

Thanks!

1 Like