KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Implode Array into String With Native 4D Code
PRODUCT: 4D | VERSION: 16R5 | PLATFORM: Mac & Win
Published On: February 9, 2018

Below is a utility method to implode an array into a single string usign native 4D code with collections. This is only available in v16R5 and above.

// Method:
// Util_Implode
//
// Inputs:
// $1 - Text Array to Implode
// $2 - Deliminating String, Optional defaults to empty string
//
// Output:
// $0 - Text result
//
//---------------------------------------------------------

C_POINTER($1;$stringArr_p)
C_TEXT($2;$delim_t)

C_TEXT($0;$result_t)

C_COLLECTION($stringArr_c)

If (Count parameters>0)
   $stringArr_p:=$1
   If (Count parameters=2)
      $delim_t:=$2
   Else
      $delim_t:=""
   End if
   
   $stringArr_c:=New collection
   
   ARRAY TO COLLECTION($stringArr_c;$stringArr_p->)
   $result_t:=$stringArr_c.join($delim_t)
   
   $0:=$result_t
End if


Below are some examples of the method in use.

ARRAY TEXT($arrTest;3)

$arrTest{1}:="a"
$arrTest{2}:="b"
$arrTest{3}:="c"

$res1:=Util_Implode (->$arrTest;";")
$res2:=Util_Implode (->$arrTest;" and ")
$res3:=Util_Implode (->$arrTest)

TRACE


The results of the above are: