This is an example program for a findnext() function. It would be normally used after the find() function uploaded previously. For example, if you have a CSV (comma separated values) string and you want to retrieve the data between two commas, you could get the location of the first delimiter and then pass it to the findnext() function. It will start searching at the next character after the provided location and return the location of the next delimiter
a short example would be
dim a,b as integer
a=find(MyString,",",3)
b=findnext(MyString,a,",")
'Program: FindNextFunctionExample
' Findnext() finds the 1st location of a character
' after a starting location
'=====================================
' find function
'==================================================
function findnext(byval SearchString as string, byval StartLoc as integer, byval SearchChar as string) as integer
if len(SearchString) = 0or StartLoc = 0 or len(SearchChar) = 0 then return 0
dim sc as integer = asc(SearchChar)
for Ctr as integer = StartLoc to len(SearchString) - 1
if SearchString[Ctr] = sc then Return Ctr + 1
next
Return 0
end function
'==================================================
' Main Program
'==================================================
' Count the occurances of "|"
width 80,24
dim InputString as string = "|a|bb|ccc|dddd|eee|ff|g|"
dim StartLoc as integer = 7
dim Delimiter as string ="|"
dim Location as integer = findnext(InputString,StartLoc,Delimiter)
locate 1,1
print "findnext() function example program"
locate 3,5
print "The original string = " + InputString
locate 5,5
print "The first occurrence of """ + Delimiter + """ after position " + str(StartLoc)
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
'==================================================
' Main Program
'==================================================
' Count the occurances of "|"
width 80,24
dim InputString as string = "|a|bb|ccc|dddd|eee|ff|g|"
dim StartLoc as integer = 7
dim Delimiter as string ="|"
dim Location as integer = instr(startloc,InputString,Delimiter) 'findnext(InputString,StartLoc,Delimiter)
locate 1,1
print "findnext() function example program"
locate 3,5
print "The original string = " + InputString
locate 5,5
print "The first occurrence of """ + Delimiter + """ after position " + str(StartLoc)
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
Thanks everyone. I appreciate all the feedback. I am a retired, now hobbyist, formally professional programmer (using a different language). I haven't worked with Freebasic much, so posting these little bits of code is really helping me learn more about the language and it's capabilities.. I really like seeing the different or better ways of accomplishing the same task.