and we can return the location of the nth instance of a character in a string.
Code: Select all
'Program: FindFunctionExample
' Find() finds the nth occurence of a character in a string
' and returns the location
'==================================================
' find function
'==================================================
function find(byval SearchString as string,byval SearchChar as string,byval CharNum as integer) as integer
if len(SearchString) = 0 or len(SearchChar) = 0 or CharNum = 0 then return 0
dim CharCounter as integer
dim sc as integer = asc(SearchChar)
dim Ctr as integer
dim FoundIt as byte
for Ctr = 0 to len(SearchString) - 1
if SearchString[Ctr] = sc then CharCounter += 1
if CharCounter = CharNum then
Return Ctr + 1
exit for
end if
next
Return 0
end function
'==================================================
' Main Program
'==================================================
' Count the occurances of "|"
width 80,24
dim InputString as string = "|a|b|c|d|e|f|g|"
dim CharNum as integer = 13
dim Delimiter as string ="|"
dim Location as integer = find(InputString,Delimiter,CharNum)
locate 1,1
print "find() function example program"
locate 3,5
print "The original string = " + InputString
locate 5,5
print "Occurrence " + str(CharNum) + " of """ + Delimiter + """"
locate 6,5
if Location = 0 then
print("was not found!")
else
print("was found at position " + str(Location))
end if
locate 8,1
print "Press any key to exit " ;
sleep