Splitting and Joining Strings
The STRSPLIT function is used to break apart a string, and the STRJOIN function is used to glue together separate strings into a single string.
The STRSPLIT function uses the following syntax:
Result = STRSPLIT( String [, Pattern] )
where String is the string to be split, and Pattern is either a string of character codes used to specify the delimiter, or a regular expression, as implemented by the STREGEX function.
The STRJOIN function uses the following syntax:
Result = STRJOIN( String [, Delimiter] )
where String is the string or string array to be joined, and Delimiter is the separator string to use between the joined strings.
The following example uses STRSPLIT to extract words from a sentence into an array, modifies the array, and uses STRJOIN to rejoin the individual array elements into a new sentence:
str1 = 'Hello Cruel World' words = STRSPLIT(str1, ' ', /EXTRACT) newwords=[words[0],words[2]] PRINT, STRJOIN(newwords, ' ')
This code results in the following output:
In this example, the EXTRACT keyword caused STRSPLIT to return the substrings as array elements, rather than the default action of returning an array of character offsets indicating the position of each substring.
The STRJOIN function allows us to specify the delimiter used to join the strings. Instead of using a space as in the above example, we could use a different delimiter as follows:
str1 = 'Hello Cruel World' words = STRSPLIT(str1, ' ', /EXTRACT) newwords=[words[0],words[2]] PRINT, STRJOIN(newwords, ' Kind ')
This code results in the following output: