From Test-Scratch-Wiki

Working with strings 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 custom blocks that are easier to work with. There are two basic methods for performing this: the Variable Method and List Method.

Note 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 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 string input in the custom block that represents the character being inserted
define add (new character) at (character#) of string //"string" is text to notify you what variable you change
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 inserted
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
repeat ((length of (string)) - (character#) //repeats the amount of characters after the inserted one
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 [string v] to (changer) //stores the new string in the specified variable

add [] at () of string //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 result of adding the new character to the string
define add (new character) at (letter #) of (string) //"string" is a string input that represents the character being inserted
set [item# v] to (1) //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
insert (new character) at (letter #) of [letters v] //inserts the new character
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

add [] at () of [] //this is what the custom block looks like

See Also

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