Often, it becomes the case that it is necessary to separate a string into some number of pieces. Custom blocks allow for a flexible method to accomplish this. There are two major types of string splitting: splitting into two at a point, and splitting into a list by a certain substring (for example, by splitting a sentence into words).
Splitting into two Variables
For this script, three variables need to be defined:
(letter)— a variable used for iterating through the string(error)— a variable used to monitor errors with splitting(split 1)— the variable containing the first part of the string(split 2)— the variable containing the second part of the string
The following script splits a string into two. For example, if the string is split after letter 3, (split 1) will contain the 1st - 3rd letters, and (split 2) will contain the 4th letter onward.
define split string [string] after letter (letter)
set [letter v] to (0)
set [split 1 v] to []
set [split 2 v] to []
set [error v] to [false]
if <(letter) < ((length of (string)) + (1))> then
repeat (letter)
change [letter v] by (1)
set [split 1 v] to (join (split 1) (letter (letter) of (string)))
end
repeat ((length of (string)) - (letter))
change [letter v] by (1)
set [split 2 v] to (join (split 2) (letter (letter) of (string)))
end
else
set [error v] to [true]
end
Splitting into a List
For this script, four variables and a list need to be defined:
(i)— a variable used to iterate through the string(j)— a variable used to iterate through the delimiter(leading)— a variable that keeps track of the left side of the string(buffer)— a variable that keeps track of the matched delimiter(output::list)— a list contains the split items
The following script splits a string into a list. It takes in a substring as a delimiter, which can be any arbitrary number of characters. If the delimiter is empty, the script splits the string by each character.
An example implementation can be found here.
define split (string) by (delimiter)
delete all of [output v]
set [leading v] to []
set [i v] to (1)
repeat (length of (string))
set [leading v] to (join (leading) (letter (i) of (string)))
if <(leading) contains (delimiter)?> then
set [buffer v] to []
set [j v] to (1)
repeat ((length of (leading)) - (length of (delimiter)))
set [buffer v] to (join (buffer) (letter (j) of (leading)))
change [j v] by (1)
end
add (buffer) to [output v]
set [leading v] to []
end
change [i v] by (1)
end
if <(length of (leading)) > (0)> then
add (leading) to [output v]
end