Reg Exp
Web Design
Notes Client
Implode/Explode Functions
NOTE: Version 6 of Notes now has built-in functions that should be used instead of these functions. There is a built-in Split function which mimics the @Explode function and the Explode function written below. There is a built-in Join function which mimics the @Implode function and the Implode function written below. In fact, LotusScript aliases the value "Implode" to "Join", so the Implode function below will not work any more in version 6 since there is now a built-in function with the exact same name.

When working with LotusScript, every once in a while you need to take an array and convert it to a string, or take a string and convert it to an array, just like @Implode and @Explode. You may be thinking that the Evaluate LotusScript statement will do the trick. In most cases, it will. However, try putting this code into an agent and running it:

Dim temp As Variant
Dim i As Integer
Dim result As String
temp = Evaluate({@Explode("Hello\there~how~are~you?"; "~")})
result = ""
For i = Lbound(temp) To Ubound(temp)
   result = result & temp(i) & Chr$(10)
Msgbox result

The resulting message box will not be exactly what you expect, as can be seen in figure 1. This is because the slash is an escape character in formula language, so it is not considered as part of the string. That's why this LotusScript Explode function (and the Implode function as well) is sometimes necessary.

Here are the functions:

Function Explode(fullString As String, separator As String, empties As Integer) As Variant
   Dim fullStringLen As Integer
   Dim lastPosition As Integer
   Dim position As Integer
   Dim x As Integer
   Dim tmpArray() As String
   If empties = False Then fullString = Trim$(fullString)
   If separator = "" Then separator = " "
   fullStringLen = Len(fullString)
   lastPosition = 1
   position = Instr(fullString, separator)
   If position > 0 Then
      x = 0
      While position > 0
         x = x+1
         Redim Preserve tmpArray(x)
         ' The next entry in the array is going to be the part of the string from the
         ' end of the previous part to the start of the new part.
         tmpArray(x-1) = Mid$(fullString, lastPosition, position - lastPosition)
         If empties = False Then
            ' If the user does not want empties and there are consecutive separators,
            ' skip over the 2nd, 3rd, etc. instance of the separator.
            While Mid$(fullString, position+Len(separator), Len(separator)) _
            = Mid$(fullString, position, Len(separator))
               position = Instr(position+Len(separator), fullString, separator)
         End If
         lastPosition = position + Len(separator)
         position = Instr(position+Len(separator), fullString, separator)
      tmpArray(x) = Mid$(fullString, lastPosition)  ' Save everything after the last separator
      If empties = False And Trim(tmpArray(x)) = "" Then
         Redim Preserve tmpArray(x-1) ' Eliminate the last one if it's empty
      End If
      Redim tmpArray(0)
      tmpArray(0) = fullString
   End If
   Explode = tmpArray
End Function

Function Implode(anArray As Variant, concatenator As String) As String
   Dim tmpString As String
   If Not Isarray(anArray) Then
      tmpString = anArray
      If Ubound(anArray) = Lbound(anArray) Then
         tmpString = Trim(anArray(Lbound(anArray)))
         Forall items In anArray
            tmpString = tmpString & items & concatenator
         End Forall
         ' The concatenator was always added; the last one needs to be removed
         tmpString = Left(tmpString, Len(tmpString) - Len(concatenator))
      End If
   End If
   Implode = tmpString
End Function