RFindString()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> let's simply code it for now... :-)

Here's what I've been using for ages:

Code: Select all

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

Debug FindStringLast("123-456-789","-",1) ; Returns 8, because last dash is 8th character.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

That works perfectly, thanks PB :)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Code: Select all

Procedure.l x_rfindstring(string.s,find.s)                          ; find string find.s in string.s starting from the right side
  Protected l.l , p.l
  ;
  ; *** find a string in another string, starting from the right
  ;
  l.l = Len(find.s)
  p.l = Len(string.s)
  If l > 0 And p > 0
    p = p + l
    Repeat
      p = p - 1
    Until p.l = 0 Or Mid(string,p,l) = find
  EndIf
  ProcedureReturn p
EndProcedure
i admit it's probably not the best way to do it :-)

i think this should be a standard purebasic command though, imho etc.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

blueznl, yeah I can't dissagre there ;)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
Post Reply