From Test-Scratch-Wiki

Translate: - English - 中文 
"Length of () (block)" redirects here. For the List block, see Length of () (List block).
字串長度 ()
字串長度\( (詢問的答案) \)
類別 運算類積木
形狀 橢圓形積木
加入於 1.4

The Length of () 積木 is an 運算類積木 and a 橢圓形積木. The block reports how many characters the given 字串 contains.

Note Note: Spaces count as characters in strings.


In certain 專案, 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:

  • Checking the length of a name
詢問 [What's your name?] 並等待
說出 (字串組合 (詢問的答案) 和 (字串長度\( (詢問的答案) \))) (2) 秒
  • Checking how many digits there are in a player's score
  說出 (字串長度\( (score1) \))
  • Chatbots, where different messages can occur depending on the length of the message that was inputted
如果 <(字串長度\( (詢問的答案) \)) < (10)> 那麼 
  說出 [Hi!]
  說出 [Bye!]


Main article: List of Block Workarounds

This block can be replicated with the following code:

變數 [length v] 設為 [1]
重複直到 <(字串中第 ((length) + (1)) 字\( (thing) \)) = []> 
  變數 [length v] 改變 (1)

It can be flawed because it can be long and slow. This is also a problem:

變數 [repcount v] 設為 (0)
重複直到 <(字串中第 ((repcount) + (1)) 字\( (string) \)) = []> 
  新增項目 (字串中第 ((repcount) + (1)) 字\( (string) \)) \( [results v] \)
  變數 [repcount v] 改變 (1)
變數 [length v] 設為 (清單 [results v] 的項目數 :: list)

It takes up lots of memory and can be slow. However, both values can be saved for later.


— the version for 清單s