Manage Learn to apply best practices and optimize your operations.

Converting A String Into An Array

A LotusScript function similar to @Explode() which takes a string and turns it
into an array based of off the specified item delimiter.

Example usage:
Dim avarNew As Variant

Dim strSource As String

strSource = "Item 1, Item 2, Item 3"

avarNew = split(strSource, ",")

Print avarNew(0) ' Output: Item 1
Print avarNew(1) ' Output: Item 2
Print avarNew(2) ' Output: Item 3
Function split(strSource As String, strItemDelim As String) As Variant
' split() Function
' Copyright 1998-99 GWI Software, Inc.
' Created By Jordan Matthiesen
' Splits a source string into an array
' using the item delimiter specified.
' strSource The source, "flat", string to be split
' strItemDelim The item delimiter to use
' Variant An array containing the split string

'*** Variable Declarations ***
Dim astrReturn() As String ' The array to return

Dim intElement As Integer ' The array element currently being
Dim z_intItemCount As Integer ' The total number of elements for
the array
Dim z_intStartOfItem As Integer ' The start position of the item in
' source string.
Dim z_intEndOfItem As Integer ' The end position of the item in the
' string.

Dim z_strCurItem As String ' The currently parsed item
Dim z_strRemaining As String ' The remaining string to cycle
Dim z_strStartOfItemBack As String ' The rest of the string, from the
start of
' the item on back.
'*** End Variable Declarations ***

' Put the source string into a holder:
z_strRemaining = strSource

' Cycle through the source string and get a
' count of elements:
While (Not z_strRemaining = "")
z_intStartOfItem = Instr(1, z_strRemaining, strItemDelim)

If (z_intStartOfItem <> 0) Then
z_intItemCount = z_intItemCount + 1
z_strRemaining = Mid(z_strRemaining, z_intStartOfItem +
Len(strItemDelim), _
z_intItemCount = z_intItemCount + 1
z_strRemaining = ""
End If

' Size the return array so that it can hold
' all of the elements:
Redim astrReturn(z_intItemCount - 1)

' Reset the holder to the value of the source string:
z_strRemaining = strSource

' Cycle through the source list, parsing the
' string into individual array elements:
While (Not z_strRemaining = "")
For intElement = 0 To Ubound(astrReturn)
' Find the end of the current element:
z_intEndOfItem = Instr(1, z_strRemaining, strItemDelim)

' If there is still an item delimiter in the string, parse
' out the item, otherwise assume the remaining text is the
' last item:
If (z_intEndOfItem <> 0) Then
z_strCurItem = Mid(z_strRemaining, 1, z_intEndOfItem - 1)
astrReturn(intElement) = z_strCurItem
z_strRemaining = Mid(z_strRemaining, z_intEndOfItem +
Len(strItemDelim), _
astrReturn(intElement) = z_strRemaining
z_strRemaining = ""
End If


' Return the split array to the caller:
split = astrReturn
End Function

Dig Deeper on Domino Resources - Part 3



  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...

  • HTML cheat sheet

    This is a really cool cheat sheet if you're looking to learn more about HTML. You'll find just about everything you every wanted ...

  • Carol Woodbury: Security

    Carol Woodbury