String Search by Keaz25th Jul 2005 16:14
|
---|
Summary These functions search a string in forward or reverse for a given string from a given starting point. Originally By IanM modified and commented by Keaz. (This function is CaSe SeSi Description These functions search a string in forward or reverse for a given string from a given starting point. Originally By IanM modified and commented by Keaz. (This function is CaSe SeSiTiVe) Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com function Instr(Source as string, Search as string, StartPos as integer) ` Skip search if any values are illegal if Source = "" then exitfunction 0 if Search = "" then exitfunction 0 if StartPos < 1 then exitfunction 0 ` Cache the lengths of the strings - no need to recalculate them every loop local SourceLen as integer local SearchLen as integer SourceLen=len(Source) SearchLen=len(Search) ` One last check using the lengths for illegal search start if StartPos + SearchLen > SourceLen+1 then exitfunction 0 `Cache left char no need for redoing this every loop local Chrl as string ChrL = left$(Search, 1) ` Cache max search so you don't recalculate it every loop local MaxSearch as integer MaxSearch=SourceLen-SearchLen+1 `declare our search loop variables for speed local i as integer local j as integer local k as integer `Our search loop for i = StartPos to MaxSearch if mid$(Source, i) = ChrL ` 1st character matched - match from 2nd char onwards `k = 1 for 1 match k = 1 for j = 2 to SearchLen ` This one doesn't match - skip out of the loop if mid$(Source, i+k) <> mid$(Search, j) then exit ` Else we got another match try the next inc k next ` If matched all characters, return the match if k = SearchLen then exitfunction i endif next i ` If we got here, there is no match endfunction 0 function InstrRev(Source as string, Search as string, StartPos as integer) ` Skip search if any values are illegal if Source = "" then exitfunction 0 if Search = "" then exitfunction 0 if StartPos < 1 then exitfunction 0 ` Cache the lengths of the strings - no need to recalculate them every loop local SourceLen as integer local SearchLen as integer SourceLen=len(Source) SearchLen=len(Search) ` One last check using the lengths for illegal search start if StartPos + SearchLen > SourceLen+1 then exitfunction 0 `Cache left char no need for redoing this every loop local Chrl as string ChrL = left$(Search, 1) ` Cache max search so you don't recalculate it every loop local MaxSearch as integer MaxSearch=SourceLen-SearchLen+1 `declare our search loop variables for speed local i as integer local j as integer local k as integer `Our search loop in reverse this time for i = StartPos to 1 step -1 if mid$(Source, i) = ChrL ` 1st character matched - match from 2nd char onwards `k = 1 for 1 match k = 1 for j = 2 to SearchLen ` This one doesn't match - skip out of the loop if mid$(Source, i+k) <> mid$(Search, j) then exit ` Else we got another match try the next inc k next ` If matched all characters, return the match if k = SearchLen then exitfunction i endif next i ` If we got here, there is no match endfunction 0 |