From Test-Scratch-Wiki

With Scratch's limited string manipulation capabilities, moving part of a string to a different position within that same string requires a workaround. A 函式积木

set to "run without screen refresh" can be used to rapidly move a substring to another location in its base string. 

Programming

The following custom block will move the specified sub of a base string to a different position (index) within the body (base) string.

定義 move character (start) through (last) of (base) to position (pos)
變數 [i v] 設為 (start)
變數 [group1 v] 設為 []
重複 (((last) - (start)) + (1)) 次 
  變數 [group1 v] 設為 (字串組合 (group1) 和 (字串中第 (i) 字\( (base) \)))
  變數 [i v] 改變 (1)
end
變數 [group2 v] 設為 []
變數 [i v] 設為 [1]
重複 ((start) - (1)) 次 
  變數 [group2 v] 設為 (字串組合 (group2) 和 (字串中第 (i) 字\( (base) \)))
  變數 [i v] 改變 (1)
end
變數 [i v] 設為 ((last) + (1))
重複 ((字串長度\( (base) \)) - (last)) 次 
  變數 [group2 v] 設為 (字串組合 (group2) 和 (字串中第 (i) 字\( (base) \)))
  變數 [i v] 改變 (1)
end
變數 [compiled v] 設為 [] // will result in the finished string
變數 [i v] 設為 [1]
重複 ((pos) - (1)) 次 
  變數 [compiled v] 設為 (字串組合 (compiled) 和 (字串中第 (i) 字\( (group2) \)))
  變數 [i v] 改變 (1)
end
變數 [compiled v] 設為 (字串組合 (compiled) 和 (group1)) // inserts the moved substring to the proper position
變數 [i v] 設為 (pos)
重複 (((字串長度\( (base) \)) - (pos)) + (1)) 次 
  變數 [compiled v] 設為 (字串組合 (compiled) 和 (字串中第 (i) 字\( (group2) \)))
end

Explanation

The very first section of the base string that must be grouped unto an independent variable, called group1, is the substring of the body string that is to be removed. Afterwards, the substring is actually removed from the string, and the remaining characters are grouped into a variable called group2. To finish the movement, the variable "compiled" results with the finished strings joined together in the proper sequential order.

参见

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