How do i count the number of words in a textbox, then use that number to display on a label

35h

2 Likes

Thank you so much. I know it was something very simple but I just couldn't figure it out. Have a good day!

1 Like

what will it return if there are 2 or more space between 2 words?

1 Like

4 Likes

Add following function:


like in JS: String.replace(/ /g," ");

1 Like

Initializing variables here would cost a bit of memory, and you don't really have to do so; it would just be better off not using variables imho.

1 Like

Also can be done using regex expression. Here is an example using regex extension by @Kevinkun

1 Like

In the case of these space and double space variables, there is a benefit to the programmer and readers of the code in using well named variables for these text constants.

AI2 has an unfortunate habit of trimming blanks from the end of its text blocks, and not showing the difference between a single blank and an empty text value. For that reason, when I need a single space, I use a well named variable "space" in my blocks, valuing sanity and debug time over execution time and space.

1 Like

Yes, exactly, that was the reason for the use of a variable, only to clear up the strategy. One can adapt the function to one's favour. It's important to mention for those who didn't use the function "replace" til now that it does only work with the "while" loop.

1 Like

Interesting, thank you for posting. I didn't know that before. Had just a look to W3Schools/JS :smile:

1 Like

@Joerg_Kowalski, Here it is a general version of program code to remove more than one spaces.

and for word count, you can refer Anke's code block.

With one information, regarding space comparison

With regex expression you only need 4 blocks

1 Like

To illustrate the similarity between empty text blocks and space blocks, I made a little Blocks Editor game:

Three Block Monte, Find The Space!
Three Block Monte - Find the Space

4 Likes

:thinking: Interesting @ABG, while posting second time in this thread, I was thinking that there should be a way to distinguish between empty text box and single space text box to make the program more readable.

Yes, I thought it is concerning to word count only, but at first place I wanted to make it using available blocks only.

2 Likes

Another approach with blocks

image

2 Likes

@mareshu
Ja, thank you. As we say: "many ways to Rome"; I didn't test your version, but may be that there occurs an error if i = length (global sentence), because segment (lenght plus 1) doesn't exist so that the 2nd comparison in the "If" statement creates that error.

hahahahaha, that's it