From Test-Scratch-Wiki
Working with 字串s can be difficult with limited options, but there are almost always workarounds. In a string, an individual character can be changed. This tutorial explains the concept of changing one character to another. 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 text that describes what variable will be changed
定義 change (character#) of string to (new character) //"string" is a string variable that represents the original string to be changed 變數 [changer v] 設為 [] // this is blank 變數 [item# v] 設為 (1) // so the string transfer begins with the first character 重複 ((character#) - (1)) 次 變數 [changer v] 設為 (字串組合 (changer) 和 (字串中第 (item#) 字\( (string) \))) // adds the current character to "changer" 變數 [item# v] 改變 (1) // moves on to the next character end // repeats the amount of characters before the one going to be changed 變數 [changer v] 設為 (字串組合 (changer) 和 (new character)) // adds the new character that replaces the old one 變數 [item# v] 設為 ((character#) + (1)) // sets "item#" to the character after the changed one 重複 ((length of (string)) - (character#)) 次 //repeats the amount of characters after the changed one 變數 (changer) 設為 (字串組合 (changer) 和 (字串中第 (item#) 字\( (string) \))) // adds the current character to "changer" 變數 [item# v] 改變 (1) // moves on to the next character end 變數 [string v] 設為 (changer) // stores the new string in the specified variable change (3) of string to [h] ::custom// this is what the custom block looks like
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 string input in the custom block that represents what the character will be changed to
定義 change (character#) of string to (new character) //"string" is a string input that represents the original text before the character change 變數 [item# v] 設為 (1) // so 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) 替換第 (character#) 項於 [letters v] 成 (new character) // replaces the specified character with the new one 變數 [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 change () of string to [] ::custom// this is what the custom block looks like