Code archives/Miscellaneous/FindLastString function
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This function finds the position of the last occurrence of one string inside another string. If the search string is not found inside the string to be searched then a zero value will be returned. Note that the function is case sensitive by default. Specify 'False' for the 'is_case_sensitive' parameter for a case insensitive check. | |||||
Function FindLastString( haystack$, needle$, is_case_sensitive = True ) ; This function finds the position of the last occurrence of ; the 'needle$' string inside the 'haystack$' string. ; If the 'needle$' string is not found inside the 'haystack$' string ; then a zero value will be returned. ; Note that the function is case sensitive by default. ; Specify 'False' for the 'is_case_sensitive' parameter; ; for a case insensitive check. Local offset = 1, lastpos If is_case_sensitive = False haystack$ = Lower$( haystack$ ) needle$ = Lower$( needle$ ) EndIf Repeat offset = Instr ( haystack$, needle$, offset ) If offset lastpos = offset offset = offset + Len( needle$ ) EndIf Until offset = 0 Return lastpos End Function ; EXAMPLE USAGE Print "Case Sensitive: " + FindLastString( "sABabcABadabABab", "AB" ) Print Print "Not Case Sensitive: " + FindLastString( "sABabcABadabABab", "AB", False ) WaitKey : End |
Comments
| ||
I'm not sure if Instr is faster at ssearching than adding the length of needle each time, it may be quite dependant on the relative length of the strings. But this might be quicker, since it chops down the string eacch time, and performs another Instr |
Code Archives Forum