String Function findnext() - find the first location of a character after a location

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
dmontaine
Posts: 23
Joined: Mar 15, 2018 7:13

String Function findnext() - find the first location of a character after a location

Post by dmontaine »

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,",")

Code: Select all

'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

SARG
Posts: 1764
Joined: May 27, 2005 7:15
Location: FRANCE

Re: String Function findnext() - find the first location of a character after a location

Post by SARG »

No need this function it exists a build-in one : first = InStr( [ start, ] str, [ Any ] substring )

Code: Select all

'==================================================
' 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
dmontaine
Posts: 23
Joined: Mar 15, 2018 7:13

Re: String Function findnext() - find the first location of a character after a location

Post by dmontaine »

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.
Post Reply