'Returns NoFields delimited fields from record starting from StartField (first = 0)
Sub GetFields(Record As String,FieldDelim As String,StartField As Int,NoFields As Int) As String
Dim Count,SkipCount,ThisStart,StartPos As Int
Dim LastErrorMessage A string
'Determine the start position of the 1st required field
SkipCount=0
StartPos=0
Do While SkipCount < StartField
StartPos=Record.IndexOf2(FieldDelim,StartPos)+1
If StartPos=0 Then Exit
SkipCount=SkipCount+1
Loop
If SkipCount > 0 AND StartPos = 0 Then
LastErrorMessage="Error: StrUtilsLib GetFields: StartPos is past the end of the record"
Log(LastErrorMessage)
Return -1
End If
Count=0
ThisStart=StartPos
Do While Count < NoFields
ThisStart=Record.IndexOf2(FieldDelim,ThisStart)+1
If ThisStart=0 Then Exit
Count=Count+1
Loop
If ThisStart=0 Then
'No Fields Found
If Count = 0 AND NoFields > 1 AND SkipCount = 0 Then
LastErrorMessage="Error: StrUtilsLib GetFields: "&FieldDelim&" not found"
Log(LastErrorMessage)
Return -1
End If
'Last Delimeter is missing, return whole string
LastErrorMessage="Warning: StrUtilsLib GetFields: Missing last FieldDelim(s) '"&FieldDelim&"' Record returned from StartPos"
Log(LastErrorMessage)
Return Record.SubString(StartPos)
Else
Return Record.SubString2(StartPos,ThisStart)
End If
End Sub