cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
techtudoor
Resolver I
Resolver I

Word count for textbox

So I am building an App that has a multi-line textbox that needs to have a limit of x amount of words. I can count characters, but not sure how to do a word count to show the user the amount of words they have typed or have left.

 

Any suggestions greatly appreciated

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@techtudoor 

Consider the following formula:

   CountRows(Filter(MatchAll(yourTextInputControl.Text, "\S*(\s|\n?)"), !IsBlank(FullMatch)))

 

This will tell you how many words you have.

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

10 REPLIES 10
RandyHayes
Super User
Super User

@techtudoor 

Consider the following formula:

   CountRows(Filter(MatchAll(yourTextInputControl.Text, "\S*(\s|\n?)"), !IsBlank(FullMatch)))

 

This will tell you how many words you have.

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
timl
Super User
Super User

Hi @techtudoor 

I'd suggest pretty much the same as Randy. I usually just split the text by the space character and do a count of the result.

CountRows(Split(TextInput2.Text, " "))

 

timl_0-1614877122393.png

 

 

RandyHayes
Super User
Super User

@techtudoor 

Yep, @timl 's solution will work too, but you might have issue with that for a mult-line text as words in those that are at the end of a line are typically ended with a char(10).  The match function expression will account for that.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

@RandyHayes 

 

  That worked great, Thank you so much. Now to show my ignorance. Could you explain what "\S*(\s|\n?)" is doing? This way I can understand what I am doing rather than just typing it in.

 

Thank you

@timl 

 

Thank you, I will keep this in mind if I need to do this for a single line of text input. 

RandyHayes
Super User
Super User

@techtudoor 

This is a regular expression.   It essentially says - match any thing that is not a white space (\S) and ends with either zero or more (?) a whitespace (/s) or zero or more (?) a newline (\n)

 

So this text:

The quick 
brown fox

Is : 

\S\S\S\s\S\S\S\S\S\n\S\S\S\S\S\s\S\S\S\s
 T h e   q u i c k   b r o w n   f o x

The regular expression "\S*(\s|\n?)" says to match all of the \S's that end with \s or \n

The MatchAll function returns a table.  The table has records with two columns, the FullMatch and the character location.  We only care about the number of full matches and we first filter that to make sure we have no blank full matches, then count the number of rows.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Thanks Randy. That explanation was great. It now makes sense. Greatly appreciate you taking time to explain this. 

Can you please tell me a solution to get the number of letters instead of words?

timl
Super User
Super User

@ShaheerAhmad 

The Len function returns the number of letters/characters.

Len(TextInput1.Text)

 

Helpful resources

Announcements
Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Super User 2 - 2022 Congratulations

Welcome Super Users

The Super User program for 2022 - Season 2 has kicked off!

September Events 2022

Check out all of these events

Attend in person or online, there are incredible conferences and events happening all throughout the month of September.

Users online (3,222)