Split strings using a delimiter

Verified
Added by iNTERFACEWARE

Use the string.split() function to split a string into parts separated by a specified delimiter

Source Code
   -- split the message into individual lines "\r" separator
   local r = msg:split('\\r')
   trace(r) 

   -- allow for the \n added because we split the msg declaration over multiple lines (above)
   local r = msg:split('\\r\n')
   trace(r) 

   -- split the message into fields "|" separator
   local r = msg:split('|')
   trace(r)
Description
Use the string.split() function to split a string into parts separated by a specified delimiter
Usage Details

Use string.split() to break a string into multiple parts using a specified separator. We used an HL7 message as an example. When we declared the string the editor added newlines (\n) so our first split contained \n at the start of the split strings (which we did not want). Our second split allows for this by specifying the newline as part of the separator.

How to use the snippet:

  • Paste the split code into your script

Note: You may also be interested in the splitText() function which can be use to split a long reports into chunks of a specified length.