Page 1 of 1

TrimLeft and TrimRight

Posted: Tue Oct 19, 2010 4:48 am
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

Re: TrimLeft and TrimRight

Posted: Tue Oct 19, 2010 5:48 am
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 ^^

Re: TrimLeft and TrimRight

Posted: Tue Oct 19, 2010 5:53 am
by Mistrel
You know what.. I never knew we had those functions. :shock:

Thanks for pointing out a bug, too.

Re: TrimLeft and TrimRight

Posted: Tue Oct 19, 2010 6:00 am
by Nituvious
Mistrel, I think you single handedly are keeping Tricks 'n' Tips alive! Thank you so much for the neat code snippets!

Re: TrimLeft and TrimRight

Posted: Tue Oct 19, 2010 6:56 am
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. :)

Re: TrimLeft and TrimRight

Posted: Tue Oct 19, 2010 5:26 pm
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!

Re: TrimLeft and TrimRight

Posted: Tue Oct 19, 2010 5:40 pm
by eesau
If I understood correctly, it's not true, both ways are basically the same.

Re: TrimLeft and TrimRight

Posted: Tue Oct 19, 2010 5:47 pm
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

Re: TrimLeft and TrimRight

Posted: Tue Oct 19, 2010 6:00 pm
by eesau
A-ha, that's interesting!

Re: TrimLeft and TrimRight

Posted: Tue Oct 19, 2010 6:11 pm
by WilliamL
I've run into this very problem slowing down programs and used this work-around.

Interesting, indeed!