Page 1 of 1

FindStringRev()

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

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

Posted: Thu Sep 06, 2007 4:20 pm
by AND51
Did I understand you right that you want to find the LAST string in a string?
Then use my procedure, it has also got case-in-sensivity:

Code: Select all

Procedure.l FindLastString(String$, StringToFind$, CaseInSensitive=0) 
   Protected length=Len(StringToFind$), *position=@String$+(Len(String$)-length)*SizeOf(Character) 
   While @String$ < *position 
      If CompareMemoryString(*position, @StringToFind$, CaseInSensitive, length) = 0 
         ProcedureReturn (*position-@String$)/SizeOf(Character)+1 
      EndIf 
      *position-SizeOf(Character) 
   Wend 
   ProcedureReturn 0 
EndProcedure 


Debug FindLastString("Am Anfang sollte der Suchstring sollte stehen, um die Prozedur zu fordern!", "anfang", 1)
This procedure is taken from a german PureBoard-Contest an is IMHo the fastest procedure:
http://www.purebasic.fr/german/viewtopic.php?t=12730

Posted: Fri Sep 07, 2007 12:54 pm
by bingo
a other ... api way ...

Code: Select all

Procedure FindLastString(ss.s,search.s)
spoint.l = StrRStrI_(ss,0,search)
If spoint
ProcedureReturn = Len(ss)-Len(PeekS(spoint))
Else
ProcedureReturn 0
EndIf
EndProcedure

Debug FindLastString("Am Anfang sollte der Suchstring sollte stehen, <anfang> um die Prozedur zu fordern!", "1anfang")
Debug FindLastString("Am Anfang sollte der Suchstring sollte stehen, <anfang> um die Prozedur zu fordern!", "anfang")
http://msdn2.microsoft.com/en-us/library/ms538683.aspx

8)

Posted: Mon Sep 10, 2007 11:50 am
by MikeB
May not be quickest, but it is simple -

Code: Select all

Procedure.l laststring(a$, b$, casesense.b) ; a$ = search in,  b$ = look for
	If casesense>0 ; 0 = case sensitive, else none sensitive
		a$ = UCase(a$)
		b$ = UCase(b$)
	EndIf
	For k = 1 To Len(a$)-Len(b$)+1
		If Mid(a$, k, Len(b$)) = b$
			pos = k
		EndIf
	Next
	ProcedureReturn pos 
EndProcedure

Posted: Mon Sep 10, 2007 12:35 pm
by PB
Here's my small and quick routine:

Code: Select all

Procedure FindStringLast(string$,match$)
  pos=1 : Repeat : a=FindString(string$,match$,pos) : If a<>0 : pos=a+1 : EndIf : Until a=0
  ProcedureReturn pos-1
EndProcedure

Debug FindStringLast("123-567-90","-") ; Returns 8, because the last dash is at position 8.
Debug FindStringLast("the war of the worlds","the") ; Returns 12, because last "the" is at 12.