Microsoft

# LAMBDA Examples: Count words in a text string

This post is the second of a series where we will be sharing out examples of lambdas. This is intended to highlight lambdas we have cooked up that show the power of this new super-charged function. Additionally, you have the opportunity to engage with us on the lambdas you have built yourself and of course let us know how our own formulas could be improved.

If you didn’t catch the announcement, be sure to check out the blog post highlighting the release of this new function: LAMBDA

In today’s example we will be creating a lambda which allows you to count how many words are in a text string.

I find myself making use of this formula all the time when I need to do text scrubbing or fact checking I have edited my data correctly. Hopefully, if you’re like me and find yourself making use of this formula from time to time, it gives you a more repeatable way to call it without having to write all the logic from scratch.

## Count words in a text string

For this example, lets start with the problem we have at hand and would like to abstract as a custom function. Below you can see a series of text strings which I have picked for this example.

Breaking the problem down, what we are looking to do is find individual words in our sentence, but, what makes something a word? Well, naively speaking, we know that there is a word because there is a space before and/or after a series of characters.

Because of this, we can count the length of our string with and without spaces and then subtract the two. This should give us a valid count of how many “words” are in our text string.

To prove this, lets take a simple example of “Sally Jumps.”.

In this text string the counts are as follows and generally speaking the LEN function is good for this:

 Calculation Result Formula CountOfLength 12 =LEN("Sally Jumps.") CountOfLengthWithoutSpaces 11 =LEN("SallyJumps.")

With those two values calculated, we can simply subtract the LengthWithoutSpaces from the CountofLength and that will give us the value 1.

Now you might be thinking...

“OK, we get 1, but that is wrong… There are clearly 2 words in that sentence!”.

You would be correct and with this calculation, we will always be off by 1. This is a simple thing to fix and important to remember when authoring our final formula.

In summary, the formula we are going to build roughly looks like this:

`=COUNTOFLENGTH(<text string>) – COUNTOFLENGTH(<text string with no spaces>) + 1`

The next thing to consider is how we can calculate a version of our text with no whitespaces and thankfully there is a formula for that!

To remove whitespaces from our text string, we can use the SUBSTITUTE function to search for any spaces: " " and replace them with nothing: "" .The formula would look like the following.

`=SUBSTITUTE("Sally Jumps.", " ", "")`

If we take that formula and replace it with the "SallyJumps." portion of CountOfLengthWithoutSpaces we can calculate this without modifying the original string!

`=LEN(SUBSTITUTE("Sally Jumps.", " ", ""))`

With both problems completed, we can now begin to construct the final version of our formula.

Putting together the intermediate calculations we created, (and assuming our text-string lives in A1) gives us the following solution:

`=LEN(A1) – LEN(SUBSTITUTE(A1, " ", "")) + 1`

Moving along, all that is left to do, is encode this formula as a LAMBDA so we can use it later without having to think through this problem again!

In this case, our LAMBDA will only take one input, the text string, and will need to define two names within the LET that map to our intermediate calculations. The formula looks like the following:

`=LAMBDA(textString, LET(     countOfLength, LEN(textString),     countOfLengthWithoutSpaces, LEN(SUBSTITUTE(textString, " ", "")),    countOfLength - countOfLengthWithoutSpaces + 1))`

With our formula complete, some of you may have realized there is still one thing which could go wrong with our calculations and something we forgot to consider. What I am referring to is the case where there might be extra spaces included at the end of our text string.

For example, if I had the text string, "Sally Jumps.     ", our formula would return an improper count and as such, it is necessary for us to remove any leading or trailing whitespace.

To prove this, we would get the following counts if we used our current solution on the problematic text string.

 Calculation Result Formula CountOfLength 17 =LEN("Sally Jumps.    ") CountOfLengthWithoutSpaces 11 =LEN(SUBSTITUTE("SallyJumps.    ", " ", ""))

This would ultimately return the value 7! There are definitely less than 7 words in that sentence…

Thankfully, since we made us of LET and have abstracted many of our calculations away, it becomes trivial to modify our solution. Additionally, if we happened to be using this in many places, we don’t have to track them down since, with LAMBDA, our formula is defined once and reused :smiling_face_with_smiling_eyes:

Moving along, the final thing we need to do to fix this, is make use of the TRIM function. TRIM removes leading and trailing spaces from a string. We can then make sure we call TRIM on textString before passing it into other calculations. There are a few ways you can re-author the above formula, but my solution looks like this:

`=LAMBDA(_textString, LET(     textString, TRIM(_textString),     countOfLength, LEN(textString),     countOfLengthWithoutSpaces, LEN(SUBSTITUTE(textString, " ", "")),    countOfLength - countOfLengthWithoutSpaces + 1))`

And that's it! You can now store this formula in the name manager, give it a name and call your newly created function! (In this case I chose to name it COUNTWORDS)

One last thing, I would like to point out, is how when I call the function, I make use of a range as the input into my function! I think this is a pretty cool use of dynamic arrays and makes it so I don’t even have to do a fill down and duplicate my formula per row!

We hope you found this example useful and look forward to seeing what lambdas you have cooked up on your own.

Until next time!

Chris Gross,

Program Manager Excel

2 Replies

# Re: LAMBDA Examples: Count words in a text string

As lambda example that's great. As word counter - not sure. For example, in "one - two - three" it counts 5 words. Microsoft Word - three, which meets my expectation.

# Re: LAMBDA Examples: Count words in a text string

I have wondered about how to create LAMBDA functions that take a variable number of parameters. One possibility could be to define a function that takes a variable number of single arguments instead such as,

``ADD:=LAMBDA(a,LAMBDA(b,IF(TYPE(b)=16,a,ADD(a+b))))``

This ADD function can take scalars or arrays as parameters and returns the sum of the entries across the single arguments provided a last dummy argument is inserted. For example the following formula returns 1+2+3+4+5=15:

``=ADD(1)(2)(3)(4)(5)(\)``

The same sort of method could be used to extend any function from two inputs to n-inputs. For instance the a+b operation could be modified to a function UNION(a,b) that stacks two arrays so that n arrays could be stacked together. It would be nice if there were a more intuitive way to do this however :)