- "Length of () (block)" redirects here. For the list block, see Length of () (List block).
Length of () | |
length of () | |
Category | Operators |
Type | Reporter |
Introduced in | 1.4 |
The length of () block is an operators block and a reporter block. The block reports how many characters the given string contains.
Example Uses
In certain projects, different events can happen based on the length of a value; this block is used to help.
Some common uses for the length of () block include:
- Checking the length of a name
ask [What's your name?] and wait say (join (answer) (length of (answer))) for (2) secs
- Checking how many digits there are in a player's score
forever say (length of (score1))
- Chatbots, in which different messages can occur depending on the length of the message that was inputted
if <(length of (answer)) < (10)> then say [Hi!] else say [Bye!] end
Workaround
- Main article: List of Block Workarounds
This block can be replicated with the following code:
define length of (string) set [length v] to [1] repeat until <(letter ((length) + (1)) of (string)) = []> change [length v] by (1) end
or
define length of (string) set [repcount v] to (0) delete all of [results v] repeat until <(letter ((repcount) + (1)) of (string)) = []> add (letter ((repcount) + (1)) of (string)) to [results v] change [repcount v] by (1) end set [length v] to (length of [results v])
See Also
- Length of () (List block) — the version for lists
() + () • () - () • () * () • () / () • Pick Random () to () • () < () • () = () • () > () • () and () • () or () • Not () • Join ()() • Letter () of () • Length of () • () Mod () • Round () • () of ()More blocks...
|