Seperating words from an input

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.
image

Step 2: Initialize a variable called ‘words’ to an empty list.
image

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.

image

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!

3 Likes

Nice trick with the list.

1 Like

Thanks for your feedback!

1 Like

The “make list from text” / “make text from list” block is one of my favorite blocks! :slight_smile:

Thanks for the tutorial!

Add those blocks to a function with an input and a return block and you can use it anywhere in your code.

2 Likes

On that screen, anyway! I soooo want cross-screen functions! :slight_smile:

4 Likes

Yep, me too! And also (sadly) one of the most unused, and unheard of.
And, oh, to have cross-screen functions! I suppose there is a reason (component names), but functions with return blocks should be able to do that!

1 Like

@actech posted a way to do this a while back. I’ll see if I can’t find that today!

2 Likes