Caché Basic Reference
Split
[Home] [Back] [Next]
InterSystems: The power behind what matters   
Class Reference   
Search:    

Returns a zero-based, one-dimensional array containing a specified number of substrings.
Synopsis
Split(string[,delimiter[,count[,compare]]])
Arguments
string String expression containing substrings and delimiters. If expression is a zero-length string, Split returns an empty array, that is, an array with no elements and no data.
delimiter Optional — String character used to identify substring limits. Usually a single character, but can be a multi-character string. If omitted, the space character (" ") is assumed to be the delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.
count Optional — Number of substrings to be returned. If -1, or an integer equal to or greater than number of substrings in string, all substrings are returned.
compare Optional — Numeric value indicating the kind of comparison to use when evaluating substrings. See Description section for values.
Description
The Split function splits a string into array elements. The Join function does the opposite; it joins array elements into a string.
The compare argument can have the following values:
Constant Value Description
vbBinaryCompare 0 Perform a binary comparison.
vbTextCompare 1 Perform a textual comparison.
Split and For Each
A Split function cannot be directly used as an argument of a For Each...Next statement. You must first assign the Split return value to an array variable. You can then specify this array variable as the group argument of the For Each...Next statement.
Examples
The following example uses the Split function to return an array from a string. By default, it uses the blank space as the string delimiter character.
Dim MyString,MyArray
MyString = "Caché is fun!"
MyArray = Split(MyString)
Println MyArray(0)   ' contains "Caché".
Println MyArray(1)   ' contains "is".
Println MyArray(2)   ' contains "fun!".
 
The following example demonstrates the delimiter argument. It uses a two-character delimiter. The string is split at each occurrence of the “aa” delimiter. A single “a” is treated as a literal, as is the third “a” in the substring “aaa”.
Dim MyString,MyArray
MyString = "Cachéaaisaaafun!"
MyArray = Split(MyString,"aa")
Println MyArray(0)   ' contains "Caché".
Println MyArray(1)   ' contains "is".
Println MyArray(2)   ' contains "afun!".
 
The following example demonstrates the use of the count argument. It returns only the specified number of substrings (in this case, 2) into array elements. Note that in this case only part of the string is returned.
Dim MyString, MyArray, Msg
MyString = "Caché;is;fun!"
MyArray = Split(MyString,";",2)
Println MyArray(0)   ' contains "Caché".
Println MyArray(1)   ' contains "is".
Println MyArray(2)   ' contains "".
 
The following example demonstrates the compare argument. It shows the difference between a binary comparison and a textual comparison. In a binary comparison, only the lowercase “x” is considered to be the delimiter; in a textual comparison, both “x” and “X” are treated as the delimiter character.
Dim MyString,MyArray
MyString = "CachéXisxfun!"
MyArray = Split(MyString,"x",-1,0)
Println "Binary: ",MyArray(0)   ' contains "CachéXis".
Println "Binary: ",MyArray(1)   ' contains "fun!".
MyArray = Split(MyString,"x",-1,1)
Println "Textual: ",MyArray(0)   ' contains "Caché".
Println "Textual: ",MyArray(1)   ' contains "is".
Println "Textual: ",MyArray(2)   ' contains "fun!".
 
See Also