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.

Warning 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) \))
end
  • Chatbots, where different messages can occur depending on the length of the message that was inputted
如果 <(字串長度\( (詢問的答案) \)) < (10)> 那麼 
  說出 [Hi!]
否則
  說出 [Bye!]
end

应用

Main article: List of Block Workarounds

This block can be replicated with the following code:

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

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)
end
變數 [length v] 設為 (清單 [results v] 的項目數 :: list)

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

参见