From Test-Scratch-Wiki

"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

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