From Test-Scratch-Wiki
A string is a series of computer characters, commonly known as letters, numbers, and symbols, that are used in many programming aspects. Strings can be broken apart into words with a fairly easy method. It requires two variables:
- letter#
- string (can be replaced by the answer block)
And it requires one list:
- words
Programming
The following script is used for breaking a string apart and separating the words into a list called words.
define separate (string) into words set [letter# v] to (1) //this variable will represent an individual letter in "string" delete [all v] of [words v] //clears all the words add [] to [words v] //adds a blank item as the basis for the first word repeat (length of (string)) //repeats the amount of letters in the string if <(letter (letter#) of (string)) = [ ]> then//note that a space is inserted into this string, not nothing add [] to [words v] //adds a new, blank item to the list if the current letter is a space else replace item (last v) of [words v] with (join (item (last v) of [words v]) (letter (letter#) of (string))) //adds the letter to the current word end change [letter# v] by (1) //moves on to the next letter
Note: | This same script can be used to separate a String into list items, not just on spaces. All that needs to be changed is instead of checking for a space, check for the separator. |
How the Script Works
First off, the variable letter# is set to 1, the list of words cleared, and one item resembling the first word is added to the list. Then, repeating the length of string, the script first checks if the current letter (represented by the variable letter#) is a space. If the letter is a space, it is not wanted to be included in the word, so instead of adding the space to the current word (item number 1) on the list, it creates a new item, representing the next word in the string. If the letter isn't a space, it just adds it to the current word by joining the incomplete word and the current letter. Finally, the script moves on to the next letter and repeats the process.