From Test-Scratch-Wiki
Working with 字串 can be difficult with limited options, but there are almost always workarounds. In a string, a character can be added. This tutorial explains the concept of inserting a character in a string. The scripts below are 函式积木 that are easier to work with. There are two basic methods for performing this: the 变量 Method and 清单 Method.
![]() | the scripts below should be run without screen refresh, which can be found in the custom block's edit menu, so it generates instantly |
![]() | for these two methods, the custom block input (string) must have the variable itself dragged into it for this to work, instead of the name of typing the variable in, because then the name itself is rendered as the string instead of the variable's characters |
Programming
Variables Method
In this method, assume the following:
- (changer) is the variable used to work with the transition of the character to the desired one
- (item#) is the variable used to iterate through the string
- (string) is the string input in the custom block that represents the character being inserted
定義 delete portion of (base) from (start) to (ending) 變數 [i v] 設為 [1] 變數 [result v] 設為 [] // starts blank 重複 ((start) - (1)) 次 變數 [result v] 設為 (字串組合 (result) 和 (字串中第 (i) 字\( (base) \))) 變數 [i v] 改變 (1) end 變數 [i v] 設為 ((ending) + (1)) 重複 ((字串長度\( (base) \)) - (ending)) 次 變數 [result v] 設為 (字串組合 (result) 和 (字串中第 (i) 字\( (base) \))) 變數 [i v] 改變 (1) end
List Method
For this method, assume the following:
- (item#) is the variable used to iterate through the string
- (letters) is the list which separates each letter of the string and modify it
- (string) is the result of adding the new character to the string
定義 add (new character) at (letter #) of (string) //"string" is a string input that represents the character being inserted 變數 [item# v] 設為 (1) // the transferring of the variable's letters to the list starts with the first character 刪除第 [全部 v] 項 \( [letters v] \) // clears the list of letters 重複 (字串長度\( (string) \)) 次 新增項目 (字串中第 (item#) 字\( (string) \)) \( [letters v] \) // adds the current letter as a new item to the list 變數 [item# v] 改變 (1) // moves on to the next letter end // repeats the amount of characters there are in the variable represented by the input (string) 添加 (new character) 為第 (letter #) 項\( [letters v] \) // inserts the new character 變數 [string v] 設為 (letters :: list) // the variable is then set to the list of letters. (letters) is a combination of each letter of the list in own string add [] at () of []::custom // this is what the custom block looks like