TrimLeft and TrimRight

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

TrimLeft and TrimRight

Post by Mistrel »

Pretty simple. This trims characters only from a specific side of the string.

Code: Select all

Procedure.s TrimLeft(String.s, Character.c)
  Protected *Trim.Trim_Char
  Protected Offset
  Protected i
  
  If Not String.s Or Not Character.c
    ProcedureReturn String.s
  EndIf
  
  *Trim=@String.s
  
  For i=1 To Len(String.s)
    If *Trim\Char[i-1]=Character.c
      Offset+1
    Else
      Break
    EndIf
  Next i
  
  If Not Offset
    ProcedureReturn String.s
  EndIf
  
  ProcedureReturn Mid(String.s,Offset+1,Len(String.s)-Offset)
EndProcedure

Procedure.s TrimRight(String.s, Character.c)
  Protected *Trim.Trim_Char
  Protected Offset
  Protected i
  
  If Not String.s Or Not Character.c
    ProcedureReturn String.s
  EndIf
  
  *Trim=@String.s
  
  For i=Len(String.s) To 1 Step -1
    If *Trim\Char[i-1]=Character.c
      Offset+1
    Else
      Break
    EndIf
  Next i
  
  If Not Offset
    ProcedureReturn String.s
  EndIf
  
  ProcedureReturn Mid(String.s,1,Len(String.s)-Offset)
EndProcedure
Last edited by Mistrel on Tue Oct 19, 2010 5:54 am, edited 1 time in total.
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: TrimLeft and TrimRight

Post by STARGÅTE »

What is different to LTrim() and RTrim() from PB ? Image

Code: Select all

Debug LTrim("...This is Art...", ".")
Debug RTrim("...Hello Word...", ".")
and your code ist bugy :

Code: Select all

Debug TrimLeft("...This is Art....", '.')
Debug TrimRight("...Hello Word....", '.')
is Art....
...Hello W
You forgot a break, so he counts the sign at the other end! :oops:

Code: Select all

If *Trim\Char[i-1]=Character.c
  Offset+1
Else
  Break ; << !
EndIf
And so on:
Mid(String.s,1,Len(String.s)-Offset) ===>> Left(String, Len(String.s)-Offset)
Mid(String.s,Offset+1,Len(String.s)-Offset) ===>> Right(String, Len(String.s)-Offset)

And

Code: Select all

For i=1 To Len(String.s)
is not good, because you count new each loop the length of String :x
Better:

Code: Select all

Protected Length = Len(String)
For i=1 To Length
Edit: :roll: oky ^^
Last edited by STARGÅTE on Tue Oct 19, 2010 5:55 am, edited 2 times in total.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: TrimLeft and TrimRight

Post by Mistrel »

You know what.. I never knew we had those functions. :shock:

Thanks for pointing out a bug, too.
Nituvious
Addict
Addict
Posts: 1029
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: TrimLeft and TrimRight

Post by Nituvious »

Mistrel, I think you single handedly are keeping Tricks 'n' Tips alive! Thank you so much for the neat code snippets!
▓▓▓▓▓▒▒▒▒▒░░░░░
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: TrimLeft and TrimRight

Post by Mistrel »

I have some pretty amazing stuff I can't wait to put up. But a lot of it isn't quite there yet. So there are still some exciting things to come. :)
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: TrimLeft and TrimRight

Post by WilliamL »

Code: Select all

SELECT ALL
For i=1 To Len(String.s)
is not good, because you count new each loop the length of String (length of String has to be calculated each loop of For/Next)
Better:

Code: Select all

SELECT ALL
Protected Length = Len(String)
For i=1 To Length
Is that true? That could be quite a speed difference!
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: TrimLeft and TrimRight

Post by eesau »

If I understood correctly, it's not true, both ways are basically the same.
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: TrimLeft and TrimRight

Post by STARGÅTE »

<< "Is that true?"
Yes:

@eesau: No !

Example:

Code: Select all

Procedure.i MyLen(String.s)
  Debug "Len("+String+")"
  ProcedureReturn Len(String)
EndProcedure


String.s = "Example"
For i=1 To MyLen(String)
  String = Left(String, Len(String)-1)
Next
Len(Example)
Len(Exampl)
Len(Examp)
Len(Exam)
Len(Exa)

the expression behind To run with each loop, always

Code: Select all



#Size = 100000

Space$ = Space(100)

Time = ElapsedMilliseconds()
For n = 1 To #Size
  For i = 1 To Len(Space$)
  Next
Next
Time1 = ElapsedMilliseconds()-Time



Time = ElapsedMilliseconds()
For n = 1 To #Size
  Len = Len(Space$)
  For i = 1 To Len
  Next
Next
Time2 = ElapsedMilliseconds()-Time


MessageRequester("", "Time1: "+Str(Time1)+Chr(10)+"Time2: "+Str(Time2))
Time1: 688
Time2: 16
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: TrimLeft and TrimRight

Post by eesau »

A-ha, that's interesting!
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: TrimLeft and TrimRight

Post by WilliamL »

I've run into this very problem slowing down programs and used this work-around.

Interesting, indeed!
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply