Useful if you need to find out how many delimited sub-strings are in a string. The number of sub-strings will be count()+1.
Code: Select all
'Program: CountFunctionExample
' This program demonstrates a count() function
' counts all instances of a character in a string
'==================================================
' count() function
'==================================================
function count(byval SearchString as string,byval SearchChar as string) as integer
if len(SearchString) = 0 or len(SearchChar) = 0 then return 0
dim CharCounter as integer
dim sc as integer = asc(SearchChar)
for Ctr as integer = 0 to len(SearchString) - 1
if SearchString[Ctr] = sc then CharCounter += 1
next
Return CharCounter
end function
'==================================================
' Main Program
'==================================================
' Count the occurrences of "|"
width 80,24
dim InputString as string = "a|b|c|d|e|f|g"
locate 1,1
print "count() function example program"
locate 3,5
print "The original string = " + InputString
locate 4,5
print "Intances of " + "|" + """ = " + str(count(InputString,"|"))
locate 6,1
print "Press any key to exit " ;
sleep