OpenNTF.org - the SPLIT function (unlimited)
My Links (Not logged in)
Code Bin Search
 
Hosted by Prominic.NET
Rate This Code
5 - brilliant stuff
4 - very nice
3 - average
2 - needs work
1 - bad
   OpenNTF Code Bin
About This Code
Brief Description:
the SPLIT function (unlimited) 
Rating:
Not Rated Yet 
Contributor:
Adam Fakes 
Category:
Lotusscript 
Type:
String functions 
Notes Version:
R5.x 
Last Modified:
27 Apr 2004 
OpenNTF Disclaimer

All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.

Code / Description
Function split(strSource As String,strSep As String) As Variant


Const CODE_MODULE = |split|

On Error Goto handleError
split = strSource

Dim i As Integer
Dim listResult List As String
' we are going to create a list containg the split string minus the seperator
' if we use a list fir then we don't have worry about the number of elements
' as opposed to an array

Dim intElementCount As Integer
Dim intArrayCount As Integer
Dim arrResult() As String
Dim strResult As String
Dim strChunk As String

intElementCount = 0

If Instr(strSource,strSep) > 0 Then
If Len(strSource) = Len(strSep) Then ' the source string only contains the seperator
listResult(0) = "" ' return empty string
Else
For i = 1 To Len(strSource) Step Len(strSep)
strChunk = Mid$(strSource,i,Len(strSep))
If strChunk <> strSep Then
listResult(intElementCount) = listResult(intElementCount) & strChunk
Else
intElementCount = intElementCount + 1
End If ' strChunk <> strSep
Next i
End If ' Len(strSource) = Len(strSep)
Else
listResult(0) = strSource' source string does not contain seperator
End If ' intStart > 0

intArrayCount = 0
Redim arrResult(intElementCount)
Forall sv In listResult
arrResult(intArrayCount) = sv
intArrayCount = intArrayCount + 1
End Forall

split = arrResult

Exit Function

handleError:
Print CODE_MODULE & "" & cstr(Err) & " " & cstr(Erl) & " " & Error(Err)
Exit Function

End Function

Usage / Example

dim arrPartList as Variant
dim strPartList = "part1,part2,part3,part4,part5"
dim i as Integer

arrPartList = split(strPartList,",")

for i = lbound(arrPartList) to ubound(arrPartList)
Print arrPartList(i)
next i
 Comments
Posted by Jason Levine on 11/06/2005 06:13:30 AMKudos on the script!
Thanks Adam, really useful!
I made one small modification to the line:
> listResult(0) = "" ' return empty string
I added listResult(1) = "" since, IMHO, the separator would be in fact separating two empty strings.
Just semantics :-)
-jason
 Add your comment!