FindStringRev()

Share your advanced PureBasic knowledge/code with the community.
akee
Enthusiast
Enthusiast
Posts: 498
Joined: Wed Aug 18, 2004 9:52 am
Location: Penang, Malaysia

FindStringRev()

Post 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
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

Post 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)
["1:0>1"]
MikeB
Enthusiast
Enthusiast
Posts: 183
Joined: Sun Apr 27, 2003 8:39 pm
Location: Cornwall UK

Post 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
Mike.
(I'm never going to catch up with the improvements to this program)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
Post Reply