Code: Select all
Procedure.s StringField_(StringToSearch.s, Index.i, Delimiter.s)
Protected StartPos.i=0
Protected EndPos.i=0
;/ To prevent an infinite loop
If Index<1
ProcedureReturn StringToSearch
EndIf
;/ Parameters strings cannot be blank
If StringToSearch="" Or Delimiter=""
ProcedureReturn StringToSearch;
EndIf
;/ Find the string index of previous to the next occurrence of the
;/ delimiter
For i=1 To Index-1
StartPos=FindString(StringToSearch,Delimiter,StartPos+1)
Next i
;/ If the starting match is at the first character and the index is 1 then
;/ Return an empty string
If Index=1 And StartPos=1
ProcedureReturn ""
EndIf
;/ Find the string index of the Next occurrence of the delimiter
EndPos=FindString(StringToSearch,Delimiter,StartPos+1)
;/ If no start position was found (no search was made for (1 to 0) but an
;/ end position was found then set the start position to the first character
If Not StartPos And EndPos
StartPos=1
EndIf
;/ If no End field is found then set it To the length of the search
;/ string. For fields other than the first And the last endPos is negated
;/ 1 To account For findString starting from 1 instead of 0.
If Not EndPos
EndPos=Len(StringToSearch)
Else
endPos-1
EndIf
;/ If no fields are found then Return the original search string
If Not StartPos And EndPos=Len(StringToSearch)
ProcedureReturn StringToSearch
EndIf
;/ If a field is found but the startPos is 0 then the string field index
;/ is beyond the final match. Return a blank string To identify that there
;/ are no more fields
If StartPos=0
ProcedureReturn ""
EndIf
If Index=1
ProcedureReturn Mid(StringToSearch,1,EndPos)
EndIf
ProcedureReturn Mid(StringToSearch,StartPos+Len(Delimiter),EndPos-StartPos-Len(Delimiter)+1)
EndProcedure