Page 1 of 1

New String-Related Function FieldStart()

Posted: Sat Aug 09, 2008 12:11 pm
by akj
I have two requests:

1. That StringField() be renamed to FieldString() for consistency with the names of other string functions.

2. That a new string function FieldStart() be added as it would complement the existing StringField() facility by returning the start position of each field rather than the contents. The high-level code for it would be:

Code: Select all

Procedure.l FieldStart(string$, index, separator$)
; Returns the start position of the string field at the specified index
; Consistent with the usage of StringField()
Protected field, posn = 1
For field = 0 To index-2
  posn = FindString(string$, "-", posn)+1
  If posn=1
    posn=Len(string$)+1 ; Suitable for use with Mid()
    Break
  EndIf
Next field
ProcedureReturn posn
EndProcedure

; Example:
Define x$, field
x$ = "abc-de-fgh-i--jk-" ; 1 5 8 12 14 15 18
For field = 1 To CountString(x$, "-")+1
  Debug Str(FieldStart(x$, field, "-"))+"   "+StringField(x$, field, "-")
Next field
End
This proposed new function would be most beneficial for parsing complex strings in which the delimiter/separator could appear within the fields themselves, especially in a last 'catch all' compound field.

It would also be useful for analysing strings having a mix of delimiters/separators.