PlanetSquires Forums

Support Forums => General Board => Topic started by: Paul Squires on August 19, 2015, 01:37:03 PM

Title: FF_TALLY and FF_TALLYANY
Post by: Paul Squires on August 19, 2015, 01:37:03 PM


''
''  FF_TALLY
''  Count the number of occurrences of strings within a string.
''
''  MainString is the string expression in which to count characters. 
''  MatchPattern is the string expression to count all occurrences of.
''  If MatchPattern is not present in MainString, zero Is returned.
''  When a match is found, the scan for the next match begins at the
''  position immediately following the prior match.
''  This function is case-sensitive.
''
Function FF_Tally( ByRef sMainString   As String, _
                   ByRef sMatchPattern As String _
                   ) As Integer

    Dim count As Integer
    Dim i     As Integer
   
    count = 0
    i = 1
    Do
        i = Instr(i, sMainString, sMatchPattern)
        If i > 0 Then count += 1: i += Len(sMatchPattern)
    Loop Until i = 0
   
    Return count

End Function


''
''  FF_TALLYANY
''  Count the number of occurrences of specified characters strings within a string.
''
''  MainString is the string expression in which to count characters. 
''  MatchPattern is a list of single characters to be searched for individually: a match on
''  any one of which will cause the count to be incremented for each occurrence of that
''  character. Note that repeated characters in MatchPattern will not increase the tally.
''  This function is case-sensitive.
''
Function FF_TallyAny( ByRef sMainString   As String, _
                      ByRef sMatchPattern As String _
                      ) As Integer

    Dim count As Integer
    Dim i     As Integer
    Dim y     As Integer
    Dim z     As Integer
    Dim fDuplicate As Integer
    Dim nLenMatch As Integer = Len(sMatchPattern)
   
   
    If Len(sMainString) = 0 Then Return 0

    count = 0
   
    For y = 0 To nLenMatch - 1
       ' Make sure that we have not already searched for this character. It is
       ' possible that the MatchPattern could contain multiple copies of the character.
       ' For example:  MatchPattern = "bab"  ' we would only search for 'b' once, not twice.
       fDuplicate = False
       If y > 0 Then
          For z = 0 To y - 1
             If sMatchPattern[z] = sMatchPattern[y] Then
                fDuplicate = True
                Exit For
             End If   
          Next
       End If

       If fDuplicate = False Then
          ' Now do the count
          i = 1
          Do
             i = Instr(i, sMainString, Chr(sMatchPattern[y]))
             If i > 0 Then count += 1: i += 1
          Loop Until i = 0
       End If
    Next
   
    Return count

End Function