From Test-Scratch-Wiki
Working with strings 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 custom blocks that are easier to work with. There are two basic methods for performing this: the Variable Method and List Method.
Note: | the scripts below should be run without screen refresh, which can be found in the custom block's edit menu, so it generates instantly |
Note: | 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
define change (character#) of string to (new character) //"string" is a string variable that represents the original string to be changed set [changer v] to [] //this is blank set [item# v] to (1) //so the string transfer begins with the first character repeat ((character#) - (1)) //repeats the amount of characters before the one going to be changed set [changer v] to (join (changer) (letter (item#) of (string))) //adds the current character to "changer" change [item# v] by (1) //moves on to the next character end set [changer v] to (join (changer) (new character)) //adds the new character that replaces the old one set [item# v] to ((character#) + (1)) //sets "item#" to the character after the changed one repeat ((length of (string)) - (character#) //repeats the amount of characters after the changed one set (changer) to (join (changer) (letter (item#) of (string))) //adds the current character to "changer" change [item# v] by (1) //moves on to the next character end set [string v] to (changer) //stores the new string in the specified variable change (3) of string to [h] //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
define change (character#) of string to (new character) //"string" is a string input that represents the original text before the character change set [item# v] to (1) //so the transferring of the variable's letters to the list starts with the first character delete [all v] of [letters v] //clears the list of letters repeat (length of (string)) //repeats the amount of characters there are in the variable represented by the input (string) add (letter (item#) of (string)) to [letters v] //adds the current letter as a new item to the list change [item# v] by (1) //moves on to the next letter end replace item (character#) of [letters v] with (new character) //replaces the specified character with the new one set [string v] to (letters) //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 [] //this is what the custom block looks like