udfStrCount
udfStrCount2
udfStrCount3
int udfStrCount (str, str, bln)
int udfStrCount2 (str, str, bln)
int udfStrCount3 (str, str, bln, int, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrCount (strBase, strSearch, blnMatchCase)
If !blnMatchCase
   strBase = StrLower (strBase)
   strSearch = StrLower (strSearch)
EndIf
Return (StrLen (strBase) - StrLen (StrReplace (strBase, strSearch, ""))) / StrLen (strSearch)
;..........................................................................................................................................
; This UDF udfStrCount returns the number of occurrences of a searchstring within a basestring.
;
; Adapted from WinBatch TechBase
; Article ID:   W14502
; Filename:   Number of Instances of Char in a String.txt
; File Created: 2000:03:30:11:28:01
; Last Updated: 2000:03:30:11:43:44
;
; Slightly modified by Detlev Dalitz.20020521
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrCount2 (strBase, strSearch, blnMatchCase)
intCount = 0
intPos = 0
If !!blnMatchCase
   While @TRUE
      intPos = StrIndex (strBase, strSearch, intPos + 1, @FWDSCAN)
      If intPos > 0 Then intCount = intCount + 1
         Else Return intCount
   EndWhile
Else
   While @TRUE
      intPos = StrIndexNC (strBase, strSearch, intPos + 1, @FWDSCAN)
      If intPos > 0 Then intCount = intCount + 1
         Else Return intCount
   EndWhile
EndIf
;..........................................................................................................................................
; This UDF udfStrCount2 returns the number of occurrences of a searchstring within a basestring.
;
; Detlev Dalitz.20020209
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrCount3 (strBase, strSearch, blnMatchCase, intStart, intStop)
strBase = StrSub (strBase, intStart, intStop - intStart + 1)
If strBase == "" Then Return 0
If !blnMatchCase
   strBase = StrLower (strBase)
   strSearch = StrLower (strSearch)
EndIf
Return (StrLen (strBase) - StrLen (StrReplace (strBase, strSearch, ""))) / StrLen (strSearch)
;..........................................................................................................................................
; This UDF udfStrCount3 returns the number of occurrences of a searchstring within a basestring
; in a limited region from intStart to intStop.
;
; Detlev Dalitz.20020521
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strMsgTitle = "Demo: udfStrCount (strBase, strSearch, blnMatchCase)"
strMsgText = ""

:Test1
strBase   = "abcannaDnnnannannnDndnnd"
strSearch = "dn"

blnMatchCase = @FALSE
intCount = udfStrCount (strBase, strSearch, blnMatchCase) ; 3
strMsgText = StrCat (strMsgText, @LF, "Test 1.1")
strMsgText = StrCat (strMsgText, @LF, 'udfStrCount ("', strBase, '", "', strSearch, '", @FALSE)')
strMsgText = StrCat (strMsgText, @LF, intCount, ' occurrences of "', strSearch, '"', @LF)
Message (strMsgTitle, strMsgText) ; '3 occurrences of "TEST"'

blnMatchCase = @TRUE
intCount = udfStrCount (strBase, strSearch, blnMatchCase) ; 1
strMsgText = StrCat (strMsgText, @LF, "Test 1.2")
strMsgText = StrCat (strMsgText, @LF, 'udfStrCount ("', strBase, '", "', strSearch, '", @TRUE)')
strMsgText = StrCat (strMsgText, @LF, intCount, ' occurrences of "', strSearch, '"', @LF)
Message (strMsgTitle, strMsgText) ; '1 occurrences of "TEST"'


:Test2
strBase = "TO test or NOT to test"

strSearch  = "O"
intCount   = udfStrCount2 (strBase, strSearch, @TRUE) ; 2
strMsgText = StrCat (strMsgText, @LF, "Test 2.1")
strMsgText = StrCat (strMsgText, @LF, 'udfStrCount2 ("', strBase, '", "', strSearch, '", @TRUE)')
strMsgText = StrCat (strMsgText, @LF, intCount, ' occurrences of "', strSearch, '"', @LF)
Message (strMsgTitle, strMsgText) ; '2 occurrences of "O"'

strSearch  = "O"
intCount   = udfStrCount2 (strBase, strSearch, @FALSE) ; 4
strMsgText = StrCat (strMsgText, @LF, "Test 2.2")
strMsgText = StrCat (strMsgText, @LF, 'udfStrCount2 ("', strBase, '", "', strSearch, '", @FALSE)')
strMsgText = StrCat (strMsgText, @LF, intCount, ' occurrences of "', strSearch, '"', @LF)
Message (strMsgTitle, strMsgText) ; '4 occurrences of "O"'

strSearch  = "TEST"
intCount   = udfStrCount2 (strBase, strSearch, @FALSE) ; 2
strMsgText = StrCat (strMsgText, @LF, "Test 2.3")
strMsgText = StrCat (strMsgText, @LF, 'udfStrCount2 ("', strBase, '", "', strSearch, '", @FALSE)')
strMsgText = StrCat (strMsgText, @LF, intCount, ' occurrences of "', strSearch, '"', @LF)
Message (strMsgTitle, strMsgText) ; '2 occurrences of "TEST"'


:Test3
strBase   = "TO test or NOT to test TO test or NOT to test"
strSearch = "to te"
intStart  = 10
intStop   = 30

blnMatchCase = @FALSE
intCount = udfStrCount3 (strBase, strSearch, blnMatchCase, intStart, intStop) ; 2
strMsgText = StrCat (strMsgText, @LF, "Test 3.1")
strMsgText = StrCat (strMsgText, @LF, 'udfStrCount3 ("', strBase, '", "', strSearch, '", @FALSE)', ', ', intStart, ', ', intStop)
strMsgText = StrCat (strMsgText, @LF, intCount, ' occurrences of "', strSearch, '"', @LF)
Message (strMsgTitle, strMsgText) ; '2 occurrences of "to te"'

blnMatchCase = @TRUE
intCount = udfStrCount3 (strBase, strSearch, blnMatchCase, intStart, intStop) ; 1
strMsgText = StrCat (strMsgText, @LF, "Test 3.2")
strMsgText = StrCat (strMsgText, @LF, 'udfStrCount3 ("', strBase, '", "', strSearch, '", @TRUE)', ', ', intStart, ', ', intStop)
strMsgText = StrCat (strMsgText, @LF, intCount, ' occurrences of "', strSearch, '"', @LF)
Message (strMsgTitle, strMsgText) ; '1 occurrences of "to te"'

Exit