KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Splitting Strings with Collections
PRODUCT: 4D | VERSION: 16R6 | PLATFORM: Mac & Win
Published On: February 1, 2018

With 4D v16 R6, splitting strings have become alot more easier with the new command Split string. To split strings into arrays, methods that spand multiple lines of code were needed in prior versions. With the new command along with the new COLLECTION TO ARRAY command also introduced in 4D v16 R6 using fewer lines of code in a more straightforward and elegant manner.

// Method:
// Util_StringToArray
//
// Inputs:
// $1 - String to split
// $2 - Deliminating String
// $3 - Pointer to Array to receive the results
// $4 - Boolean selector,
// True to ignore empty stings,
// defaults to False
// $5 - Boolean selector,
// True to trim space characters in substrings,
// defaults to False
//
//-----------------------------------------------------------------------------------

C_TEXT($1;$input_t)
C_TEXT($2;$delim_t)
C_POINTER($3;$resultAT_p)
C_BOOLEAN($4)
C_BOOLEAN($5)

C_LONGINT($options_l)
C_COLLECTION($substring_c)

If (Count parameters>2)
   $input_t:=$1
   $delim_t:=$2
   $resultAT_p:=$3
   $options_l:=0
   
   If (Count parameters>3)
      If($4=True)
         $options_l:=sk ignore empty strings
      End if
      If (Count parameters>4)
         If ($5=True)
            $options_l:=$options_l+sk trim spaces
         End if
      End if
   End if
   
   $substring_c:=Split string($input_t;$delim_t;$options_l)
   COLLECTION TO ARRAY($substring_c;$resultAT_p->)
End if


Below is an example of using the method:
C_TEXT($test)
ARRAY TEXT($myArray;0)
$test:="1-2-3-4-5-6-7-"
Util_StringToArray ($test;"-";->$myArray)
TRACE


Below are the results:


Below are TechTips on how to split a string in older versions of 4D: