How to count substring in string?
;==========================================================================================================================================
;   How to retrieve the number of occurrences of a substring in a string?
;
;   There are different ways to do it.
;
;   1. Let the function StrCnt count.
;   2. Calculate the difference length of the string pre/post a StrReplace.
;   3. Let the function BinaryStrCnt count.
;   4. Calculate the difference length of the string pre/post a BinaryReplace.
;==========================================================================================================================================

strString = "abcannannndnnnannnwennnannannaaaaiauahaaaazataraaabagafaaarafaaammannann"
strString = StrFill (strString, 4096)
strStringSub = "anna"

intLoopMax = 100

;..........................................................................................................................................
; Method 1.
; This method seems to be the fastest.
Exclusive (@ON)
intStart = GetTickCount ()
For intLoop = 1 To intLoopMax

   intCount = StrCnt (strString, strStringSub, 1, -1, 1)

Next
intStop = GetTickCount ()
intTicks1 = intStop - intStart
Exclusive (@OFF)

Message ("Count Substring in String", 'String:' : @TAB : '"' : strString : '"' : @LF : 'Substring:' : @TAB : '"' : strStringSub : '"' : @LF : 'Count:' : @TAB : intCount)
;..........................................................................................................................................

;..........................................................................................................................................
; Method 2.
Exclusive (@ON)
intStart = GetTickCount ()
For intLoop = 1 To intLoopMax

   intStrSubLen = StrLen (strStringSub)
   intCount = (StrLen (strString) - StrLen (StrReplace (strString, strStringSub, ""))) / intStrSubLen

Next
intStop = GetTickCount ()
intTicks2 = intStop - intStart
Exclusive (@OFF)

Message ("Count Substring in String", 'String:' : @TAB : '"' : strString : '"' : @LF : 'Substring:' : @TAB : '"' : strStringSub : '"' : @LF : 'Count:' : @TAB : intCount)
;..........................................................................................................................................

;..........................................................................................................................................
; Method 3.
Exclusive (@ON)
intStart = GetTickCount ()
For intLoop = 1 To intLoopMax

   intStrLen = StrLen (strString)
   hdlBB = BinaryAlloc (intStrLen)
   BinaryPokeStr (hdlBB, 0, strString)
   intCount = BinaryStrCnt (hdlBB, 0, intStrLen - 1, strStringSub)
   hdlBB = BinaryFree (hdlBB)

Next
intStop = GetTickCount ()
intTicks3 = intStop - intStart
Exclusive (@OFF)
Message ("Count Substring in String", 'String:' : @TAB : '"' : strString : '"' : @LF : 'Substring:' : @TAB : '"' : strStringSub : '"' : @LF : 'Count:' : @TAB : intCount)
;..........................................................................................................................................

;..........................................................................................................................................
; Method 4.
Exclusive (@ON)
intStart = GetTickCount ()
For intLoop = 1 To intLoopMax

   intStrLen = StrLen (strString)
   intStrSubLen = StrLen (strStringSub)
   hdlBB = BinaryAlloc (intStrLen)
   BinaryPokeStr (hdlBB, 0, strString)
   intCount = BinaryReplace (hdlBB, strStringSub, strStringSub, @TRUE)
   hdlBB = BinaryFree (hdlBB)

Next
intStop = GetTickCount ()
intTicks4 = intStop - intStart
Exclusive (@OFF)
Message ("Count Substring in String", 'String:' : @TAB : '"' : strString : '"' : @LF : 'Substring:' : @TAB : '"' : strStringSub : '"' : @LF : 'Count:' : @TAB : intCount)
;..........................................................................................................................................


;..........................................................................................................................................
; Result
Decimals (0)
intMax = 0
intTestMin = 1
intTestMax = 4
For intI = intTestMin To intTestMax
   intMax = Max (intMax, intTicks%intI%)
   ;intMax = intMax + intTicks%intI%
Next
For intI = intTestMin To intTestMax
   intPct%intI% = 100.0 * intTicks%intI% / Max (1, intMax)
Next

strMsgTitle = "How to count a substring in a string"
strMsgText = ""
For intI = intTestMin To intTestMax
   strMsgText = strMsgText : "Test " : intI : @TAB : intTicks%intI% : @TAB : intPct%intI% : ' %%' : @LF
Next
Message (strMsgTitle, strMsgText)
ClipPut (strMsgTitle : @LF : '"' : strStringSub : '"' : @LF : strMsgText)
Exit
;..........................................................................................................................................
;   How to count a substring in a string
;   "a"
;   Test 1   31   33 %
;   Test 2   79   84 %
;   Test 3   62   66 %
;   Test 4   94   100 %
;
;   How to count a substring in a string
;   "anna"
;   Test 1   15   19 %
;   Test 2   62   79 %
;   Test 3   78   100 %
;   Test 4   78   100 %
;..........................................................................................................................................
; Detlev Dalitz.20090710.
;==========================================================================================================================================