Lots of my apps required dealing with the (x)th word in an input, and counting the number of words in an input, and so on. I thought that would be possible with a block, but turned out you could only count characters. So I tried and tried to devise my own method, and in the end, when I found it, it was surprisingly simple. So here I am writing this post to show you how to do it yourself:
Every word is separated from the other words by a space, and we use that to form our solution.
For this example, Iâm creating an app that tells the user the number of words in his/her text, and the number of characters.
This is my design:
I have a text input at the top(Title of your story), that doesnât make any difference. I have another input(Write your story here), and it is from this inputâs text that weâll be using.\
Underneath that, I have a row. In that row is 1 label, called Label Words, with the text 0. The one beside has the text âWords,â. Beside that is another label with the text 0, called Label Characters, and after that is the last label, with the text âCharactersâ.
Our basic idea is to: make a list from our text inputâs text with the delimiter (space), and saving it in a variable. That means we are inserting each word into a list. Then, we need to find out the number of words. Since each word is a separate list item, that means we need to find the length of our list. We also want to display the number of characters. And, we want this all to happen whenever the user writes something new or deletes some text.
Blocks:
Step 1: Initialize an app variable called âCharactersâ to an empty text box. This is meant to store the number of characters.
Step 2: Initialize a variable called âwordsâ to an empty list.
Step 3: SInce we want this to display the words and characters everytime the user types something in our input, we will use the When Text Input Changes block.
Step 4: We want to set out variable âwordsâ to a list made from the delimiter (space).
Step 5: We want to display the number of words by displaying the length of our variable.
Step 6: Now, we want to set our characters variable to the number of characters in the userâs text. This is simple:
Step 7: We want to display our variable characters in our label.
And youâre done!