FF_EXTRACT and FF_EXTRACTANY

Started by Paul Squires, August 19, 2015, 01:40:36 PM

Previous topic - Next topic

Paul Squires




''
''  FF_EXTRACT
''  Extract characters from a string up to a character or group of characters.
''  Compliment function to FF_REMAIN.
''
''  Returns a substring of MainString starting with its first character (or the
''  character specified by nStart) and up to (but not including) the first
''  occurrence of MatchPattern.
''  If MatchPattern is not present in MainString (or is null) then all
''  of MainString is returned from the nStart position.
''  This function is case-sensitive.
''
Function FF_Extract( ByRef nStart        As Integer, _
                     ByRef sMainString   As String, _
                     ByRef sMatchPattern As String _
                     ) As String

    Dim nLenMain As Integer = Len(sMainString)             
    Dim i As Integer
   
    If (nStart = 0) Or (nStart > nLenMain) Then Return ""
    If nStart < 0 Then nStart = nLenMain + nStart + 1
   
    i = Instr(nStart, sMainString, sMatchPattern)
    If i Then
       Function = Mid(sMainString, nStart, i-nStart )
    Else
       Function = Mid(sMainString, nStart)
    End If
End Function


''
''  FF_EXTRACTANY
''  Extract characters from a string up to a specific character.
''
''  Returns a substring of MainString starting with its first character (or the
''  character specified by nStart) and up to (but not including) the first
''  occurrence of MatchPattern.
''  MatchPattern specifies a list of single characters to be searched for
''  individually, a match on any one of which will cause the extract operation
''  to be performed up to that character.
''  If MatchPattern is not present in MainString (or is null) then all
''  of MainString is returned.
''  This function is case-sensitive.
''
Function FF_ExtractAny( ByRef nStart        As Integer, _
                        ByRef sMainString   As String, _
                        ByRef sMatchPattern As String _
                        ) As String

    Dim nLenMain  As Integer = Len(sMainString)
    Dim nLenMatch As Integer = Len(sMatchPattern)
   
    Dim y As Integer
    Dim i As Integer
   
    If (nStart = 0) Or (nStart > nLenMain) Then Return ""
    If nStart < 0 Then nStart = nLenMain + nStart + 1
   
    ' Make nStart zero based because of pointer indexing
    nStart = nStart - 1
   
    ' Use string pointer indexing for speed
    For i = nStart To nLenMain - 1
       For y = 0 To nLenMatch - 1
          If sMainString[i] = sMatchPattern[y] Then 
             ' Convert nStart back to one based
             nStart = nStart + 1
             Return Mid(sMainString, nStart, i-nStart+1)
          End If   
       Next
    Next
   
    Function = ""
End Function


Paul Squires
PlanetSquires Software