The function SPLIT takes two strings, base_string and separator, and returns a list of strings in which each item is a fragment of the string making up base_string, cleaved at instances where separator appeared.
This function takes two strings as input: the first is the base string, and the second is the separator. It then splits the base string at each instance where the separator appears and outputs the result as a list of strings. This list will not include the separator within any of the strings that make up its items.
SPLIT(base_string, separator) -> list_of_strings
- base_string (required, type: string)
- The string to split.
- separator (required, type: string)
- The string that designates which subcomponents within the string base_string will be cleaved, leaving the remaining string fragments to be outputted as items in a list.
- list_of_strings (type: list)
- The list of strings remaining after taking the base string and cleaving it at instances where the separator string appeared.
SPLIT("This is the string to split", " ") -> ["This", "is", "the", "string", "to", "split"]
SPLIT("This is the string to split", "This is the string to split") -> ["", ""]
SPLIT("This is the string to split", "This is the string to split and more") -> ["This is the string to split"]
SPLIT("This is the string to split", "coconuts") -> ["This is the string to split"]
SPLIT("This is a string", "") -> ["T", "h", "i", "s", " ", "i", "s", " ", "a", " ", "s", "t", "r", "i", "n", "g" ]
long_string = "We want to split this long string in sort of a strange way."
short_string = "t t"
SPLIT(long_string, short_string) -> ["We wan", "o spli", "his long string in sort of a strange way."]
DiscussionAs a rule of thumb, when you call on the SPLIT function, you most likely want to input a separator that is shorter than the base_string, though, as demonstrated above, this is not technically a requirement. If you input a separator that is longer than base_string, you will get valid output: a list containing a single item: base_string, entirely unmodified. While this may be unavoidable if looping through several variables as input, if the goal is only to put base_string into a list, a list literal is a more efficient way to do so. That is, creating a list literal [base_string] will have the same effect and take less computing time.
SPLIT can be thought of as the opposite of the JOIN function. That is, for any two strings, let's call them big_string and little_string, you can use JOIN to undo what SPLIT did, like so:
JOIN(SPLIT(big_string, little_string), little_string) -> big_string
In other words, SPLIT takes a string with (potential) instances of a separator between them, cleaves the separator, and returns the remaining, separated strings as a list, while JOIN takes a list of strings and combines them into a single string with a separator string marking where each combination occurred.
To walk through this step by step, consider the following example, where big_string is "This is a string" and little_string is " ":
JOIN(SPLIT("This is a string", " "), " ")
The inner function, SPLIT, takes the string "This is a string," cleaves out the spaces, and outputs the remaining fragments as strings in a list:
(SPLIT("This is a string", " ") - ["This," "is", "a", "string"]
The outer function then takes the output of this inner function and combines all the strings in the list, a space now between each of them – " " being the string we just cleaved from the original, longer string – into a single string. Because the same string (" ") was used as the second input value for both functions, the output matches the original input list exactly. In other words, JOIN reinserted the spaces that SPLIT just removed:
JOIN(["This," "is", "a", "string"], " ") -> "This is a string"