KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to parse a string into multiple array elements
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: September 3, 2009

You may find yourself in a situation where you have a string of values (separated by a delimiter of some type) that you want parsed into multiple elements of an array. Surely you could loop through the string and parse out the elements yourself. However it is much easier when there is already code written that is available to simply call as utility methods.

Here are two methods to help with this task:

Method: UTIL_EscapeString

` ----------------------------------------------------
` Method: UTIL_EscapeString
` Description
` This method will place \\ (escape) in front of all provided
` characters in the given string in $1
`
` Parameters
` $1 - Given String
` ${2} - Characters or string to be escaped
`
` Return
` $0 - Result String
` ----------------------------------------------------


C_TEXT($0;$1;${2};$String_t)
C_LONGINT($param_l;$i)
$param_l:=Count parameters
If ($param_l>1)
 $String_t:=$1
 For ($i;2;$param_l)
  If (Position(${$i};$String_t)>0)
   $String_t:=Replace string($String_t;${$i};"\\"+${$i})
  End if
 End for
End if

$0:=$String_t


Method: UTIL_GetStringToken
` ----------------------------------------------------
` Method: UTIL_GetStringToken
` Description
` Get the string from a given string into an array
`
` Parameters
` $1 - String to be parsed
` $2 - Delimiter
` $3 - Pointer to a text array
` $4 - True if you want to include the last "" string, e.g. AAA;BBB;
` You will get a{1}="AAA", a{2}="BBB", a{3}=""
` False if you do not want to include the last "" string, e.g. AAA;BBB;
` You will get a{1}="AAA", a{2}="BBB"
` ----------------------------------------------------


C_TEXT($1;$2;$stringBuffer_t;$delim_t)
C_POINTER($3;$tokenList_at)
C_BOOLEAN($4;$empstrincluded_b;$match_b)
C_LONGINT($start_l;$foundat_l;$length_l)
If (Count parameters>=3)
 $stringBuffer_t:=$1
 $delim_t:=$2
 $tokenList_at:=$3
 If (Count parameters>=4)
  $empstrincluded_b:=$4
 End if
 If (Size of array($tokenList_at->)>0)
  DELETE FROM ARRAY($tokenList_at->;1;Size of array($tokenList_at->))
 End if
 If (Length($stringBuffer_t)>0)
  $delim_t:=UTIL_EscapeString ($delim_t;"?";"(";")";"{";"}";"[";"]";"*";"+")
  $start_l:=1
  Repeat
   $match_b:=Match regex($delim_t;$stringBuffer_t;$start_l;$foundat_l;$length_l)
   Case of
    : ($start_l<$foundat_l)  ` Not Empty String Token
       APPEND TO ARRAY($tokenList_at->;Substring($stringBuffer_t;$start_l;$foundat_l-$start_l))
    : ($start_l=$foundat_l) & ($empstrincluded_b)  ` Empty String Token
       APPEND TO ARRAY($tokenList_at->;"")
    : ($start_l#$foundat_l) & ($start_l<=Length($stringBuffer_t))  ` Last Token
       APPEND TO ARRAY($tokenList_at->;Substring($stringBuffer_t;$start_l))
    : ($empstrincluded_b)  ` Last Token is an Empty String
       APPEND TO ARRAY($tokenList_at->;"")
   End case
   $start_l:=$foundat_l+$length_l
  Until ($length_l=0)
 End if
End if




Here are a couple of usage examples. Example 1:

C_TEXT($test)
ARRAY TEXT($myArray;0)
$test:="1-2-3-4-5-6-7-"
UTIL_GetStringToken ($test;"-";->$myArray;False)


The above usage example will take the string "1-2-3-4-5-6-7-" and break it up by the deleimter "-" into elements of the array $myArray. The last parameter False tells the parser to not include empty values. The array would look like this:

` $myArray has 7 elements
  $myArray{1} = "1"
  $myArray{2} = "2"
  $myArray{3} = "3"
  $myArray{4} = "4"
  $myArray{5} = "5"
  $myArray{6} = "6"
  $myArray{7} = "7"


Example 2:

C_TEXT($test)
ARRAY TEXT($myArray;0)
$test:="1-2-3-4-5-6-7-"
UTIL_GetStringToken ($test;"-";->$myArray;True)


The above usage example will take the string "1-2-3-4-5-6-7-" and break it up by the deleimter "-" into elements of the array $myArray. The last parameter True tells the parser to include empty values. The array would look like this:

` $myArray has 8 elements
  $myArray{1} = "1"
  $myArray{2} = "2"
  $myArray{3} = "3"
  $myArray{4} = "4"
  $myArray{5} = "5"
  $myArray{6} = "6"
  $myArray{7} = "7"
  $myArray{8} = ""

Commented by Luis on September 11, 2009 at 4:41 PM
This routine is quite useful in many different data management applications. Data that comes separated by delimeters is common in personal information; i.e. social security numbers, phone numbers, addresses, email, etc. as well as corporate information; i.e. tax information, url, ip addresses, etc.
Any data with a delimiter and a pattern can be manipulated much easier when parsed into array elements.