FindStringRev()
Posted: Wed Sep 05, 2007 3:58 am
I miss InStrRev() from VB and I noticed that there is no FindStrRev() in PB. Well that is no longer true now.

Code: Select all
Procedure FindStringRev(StringToSearch$, StringToFind$, StartPosition = -1)
Protected LenOfStrToFind, FoundPos
FoundPos = 0
LenOfStrToFind = Len(StringToFind$)
If Len(StringToSearch$) > 0 And LenOfStrToFind > 0
If StartPosition = -1
StartPosition = Len(StringToSearch$)
EndIf
StartPosition = StartPosition - LenOfStrToFind + 1
While StartPosition > 0
If Mid(StringtoSearch$, StartPosition, LenOfStrToFind) = StringToFind$
FoundPos = StartPosition
Break
EndIf
StartPosition = StartPosition - 1
Wend
EndIf
ProcedureReturn FoundPos
EndProcedure
a.s = "The quick brown fox jumped over the lazy dog."
h.s = "hello hurrah hello hurrah hello hurrah hello hurrah"
Debug FindStringRev(a, "fox")
Debug FindStringRev(a, "mule")
Debug FindStringRev(a, "DOG")
Debug FindStringRev(a, "cow")
Debug FindStringRev(a, "T")
Debug FindStringRev(a, ".")
Debug ""
pos = Len(h)
While pos > 0
pos = FindStringRev(h, "hello", pos)
Debug pos
pos = pos - 1
Wend
Debug ""
pos = Len(h)
While pos > 0
pos = FindStringRev(h, "hurrah", pos)
Debug pos
pos = pos - 1
Wend