November 28, 2020, 02:11:03 AM

### Author Topic: [bb] FindLastString function by Zethrax [ 1+ years ago ]  (Read 479 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] FindLastString function by Zethrax [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : FindLastString function
Author : Zethrax
Posted : 1+ years ago

Description : 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.

Code :
Code: BlitzBasic
1. Function FindLastString( haystack\$, needle\$, is_case_sensitive = True )
2.         ; This function finds the position of the last occurrence of
3.         ; the 'needle\$' string inside the 'haystack\$' string.
4.
5.         ; If the 'needle\$' string is not found inside the 'haystack\$' string
6.         ; then a zero value will be returned.
7.
8.         ; Note that the function is case sensitive by default.
9.         ; Specify 'False' for the 'is_case_sensitive' parameter;
10.         ; for a case insensitive check.
11.
12.         Local offset = 1, lastpos
13.
14.         If is_case_sensitive = False
15.                 haystack\$ = Lower\$( haystack\$ )
16.                 needle\$ = Lower\$( needle\$ )
17.         EndIf
18.
19.         Repeat
20.
21.                 offset = Instr ( haystack\$, needle\$, offset )
22.
23.                 If offset
24.                         lastpos = offset
25.                         offset = offset + Len( needle\$ )
26.                 EndIf
27.
28.         Until offset = 0
29.
30.         Return lastpos
31. End Function
32.
33.
34. ; EXAMPLE USAGE
35.
36.
37. Print "Case Sensitive: " + FindLastString( "sABabcABadabABab", "AB" )
38. Print
39. Print "Not Case Sensitive: " + FindLastString( "sABabcABadabABab", "AB", False )
40.
41. WaitKey : End

`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. If (Not(is_case_sensitive)) haystack = Lower( haystack\$ ) needle = Lower( needle ) EndIf While (Instr(haystack,needle)) lastpos=Instr(haystack,needle) offset=offset+lastpos haystack=Right(haystack,Len(haystack)-lastpos) Wend Return offset End Function`