(Redirected from Letter () of () (block))

Letter () of ()
letter () of ()
Category Operators
Type Reporter
Introduced in 27Feb09 (1.4 Alpha)

The Letter () of () block is an Operators block and a Reporter block. The block reports the specified character of the given text. Even though the block says "letter", it will report all characters, including letters, numbers, symbols, and even spaces. If the inputted number is out of range, i.e. it is less than 1 or greater than the length of the text, the block will return an empty string.

Example Uses

  • Get a specific letter in a username
if <(letter (1) of (username)) = [a]> then
  say [The first letter of your username is a.]
end
  • Finding something about the user's answer given by the answer block.
if <(letter (length of (answer)) of (answer)) = [?]> then // identifying the last character of a string
  think [Thinking of a response to a question...] for (3) seconds
end
  • Writing the time with costumes used as the digits
switch costume to (letter (1) of (current [second v]))
set [location name v] to (join (join (letter (1) of (save)) (letter (2) of (save))) (letter (3) of (save)))
  • Detecting a key that is not in the drop-down menu of the Key () Pressed? block
if <key (letter (1) of [.]) pressed?> then
  set [text v] to (join (text) [.])
end

Variations

Some Scratch Modifications have a block that lets users choose how many characters in a string to report. This has been requested to be added to the Scratch code editor.[1] If added it could look like this: (letters (1) to (3) of [world]::operators)

However, it can be replicated with the following code:

letters [1] to [3] of [world]
define letters (start) to (end) of (string)
set [result v] to [] // result will be stored in this variable
set [i v] to (start) // a counter for the current letter
repeat (((end) - (start)) + (1))
  set [result v] to (join (result) (letter (i) of (string))) // join current letter to result
  change [i v] by (1) // increase counter to go to the next letter
end

See Also

References

Cookies help us deliver our services. By using our services, you agree to our use of cookies.