StrSplitToArray
See Also: Array Functions, Array Variable Assignments, String Functions, Working with Arrays, StrSplitToArray
Purpose
Splits the contents of a single String into the elements of a String array.
Return Type
Syntax
StrSplitToArray( {StringVar}, {Delimiter} )
Where:
- {StringVar}: The DataFlex String containing the elements to split into String array elements.
- {Delimiter}: The character used to separate the sub-strings in the source string. Can be any ASCII character, e.g., comma (,), period (.), etc.
What it Does
StrSplitToArray separates sub-strings from a single string, separated by the specified delimiter, into elements in a String array.
Example
This sample separates the sub-strings (in this case, 4 names), separated by commas, into the first 4 elements in the String array Flintstones. The result will be:
- Flintstones[0]: Fred
- Flintstones[1]: Wilma
- Flintstones[2]: Betty
- Flintstones[3]: Barney
Procedure Test
String[] Flintstones
String sNames
Move "Fred,Wilma,Betty,Barney" to sNames
Move (StrSplitToArray(sNames, ",")) to Flintstones
End_Procedure
Note that the resulting String array elements are in the same order as the sub-strings in the source string. If you would like the resulting String array to be sorted, you need to sort the array (using SortArray) after calling StrSplitToArray.
The result will be:
- Flintstones[0]: Barney
- Flintstones[1]: Betty
- Flintstones[2]: Fred
- Flintstones[3]: Wilma
Procedure Test
String[] Flintstones
String sNames
Move "Fred,Wilma,Betty,Barney" to sNames
Move (StrSplitToArray(sNames, ",")) to Flintstones
Move (SortArray(Flintstones)) to Flintstones
End_Procedure
Array Dimensions
You can use static arrays, but if your source string {StringVar} contains more elements than the target array, it will trigger an error:
Path\ProgramName.exe
Illegal Datatype Conversion. Incompatible size of 0. dimension - Cannot convert array of 'STRING[]' to array of 'STRING[3]'
Error: 4381
We recommend using dynamic arrays instead.