
str_split
Splits a string into an array of strings given one or more delimiters.
Available in version 5.2.0 and later.
Prototype
function str_split ( string_val [1] : string, delimiter [1] : string ) return_val [*] : string
Arguments
string_valThe input string to split.
delimiterA single string containing one or more delimiters to separate the string by.
Return value
An array of string(s).
Description
This function splits a single string based on a set of delimiters. If the string to be split contains consecutive delimiters, i.e. "1,2,3,,,4", then they will be treated as one delimiter. That is, you will get back the values 1, 2, and 4. If you want these values to be set to missing instead, for example (/1, 2, -999, -999, 4/), use str_split_csv.
See Also
Examples
Example 1a
Split a string according to a single space:
str = "NCL has many features common to modern programming languages, including types, variables, and more." strs = str_split(str, " ") print("'" + strs + "'")
Output will be:
(0) 'NCL' (1) 'has' (2) 'many' (3) 'features' (4) 'common' (5) 'to' (6) 'modern' (7) 'programming' (8) 'languages,' (9) 'including' (10) 'types,' (11) 'variables,' (12) 'and' (13) 'more.'
Example 1b
To additionally remove all the punctuation from the previous example, use the delimter string " ,."
str = "NCL has many features common to modern programming languages, including types, variables, and more." strs = str_split(str, " ,.") print("'" + strs + "'")
Output will be:
(0) 'NCL' (1) 'has' (2) 'many' (3) 'features' (4) 'common' (5) 'to' (6) 'modern' (7) 'programming' (8) 'languages' (9) 'including' (10) 'types' (11) 'variables' (12) 'and' (13) 'more'
Example 2
Note that consecutive delimiters (in this case, spaces) will be ignored:
str = "NCL has many features common to modern programming languages." strs = str_split(str, " ") print("'" + strs + "'")
Output will be:
(0) 'NCL' (1) 'has' (2) 'many' (3) 'features' (4) 'common' (5) 'to' (6) 'modern' (7) 'programming' (8) 'languages.'
Example 3
Tabs and spaces are considered unique from each other. If you have a mix of tabs and spaces in your string, you can use a delimiter with a single tab and space to split your string.
a = "This is a string with tabs and spaces." space = " " tab = str_get_tab() delimiter = space + tab print("str_split with space delimiter") print("'" + str_split(a,space) + "'") print("") print("str_split with tab delimiter") print("'" + str_split(a,tab) + "'") print("") print("str_split with space and tab delimiter") print("'" + str_split(a,tab_space) + "'") print("")
Output will be:
(0) str_split with space delimiter (0) 'This is ' (1) 'a' (2) 'string with' (3) 'tabs' (4) 'and' (5) 'spaces.' (0) (0) str_split with tab delimiter (0) 'This' (1) 'is' (2) ' a string' (3) 'with tabs and spaces.' (0) (0) str_split with space and tab delimiter (0) 'This' (1) 'is' (2) 'a' (3) 'string' (4) 'with' (5) 'tabs' (6) 'and' (7) 'spaces.'