Trim() enhancement
Trim() enhancement
How about allowing strings greater than 1 in length in the 'character to trim' argument, then trimming any of the characters from the target?
So that: Trim("<hello>", "<>")
Would return "hello"?
This would be more powerful and keep the original functionality perfectly intact.
So that: Trim("<hello>", "<>")
Would return "hello"?
This would be more powerful and keep the original functionality perfectly intact.
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Trim() enhancement
Well, PB has the Regular Expression lib for that. http://www.purebasic.com/documentation/ ... index.html
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
-
- Addict
- Posts: 4779
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Trim() enhancement
Yes, that would be very useful. E.g. PowerBasic has this feature for ages.paulr wrote:How about allowing strings greater than 1 in length in the 'character to trim' argument, then trimming any of the characters from the target?
( Until this will be implemented in PureBasic, you can use self-written functions for this. )
Re: Trim() enhancement
> This would be more powerful
But what would it do to this?
But what would it do to this?
Code: Select all
a$=Trim("<>hello<>","<>")
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
Re: Trim() enhancement
@PBPB wrote:> This would be more powerful
But what would it do to this?
Code: Select all
a$=Trim("<>hello<>","<>")
You could do this:
Macro myTrim(st, remst)
Trim(ReplaceString(st, remst,""),"")
EndMacro
a$ = "<>hello<>"
Debug myTrim(a$, "<>")
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Re: Trim() enhancement
What if the string is "<>he<>llo<>"? 

Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
-
- Addict
- Posts: 4779
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Trim() enhancement
See the link in the post directly above your post ...PB wrote:> This would be more powerful
But what would it do to this?
Code: Select all
a$=Trim("<>hello<>","<>")
-
- Addict
- Posts: 4779
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Trim() enhancement
No. As Shield already indicated, that's not a Trim() function.DK_PETER wrote:@PB
You could do this:
Macro myTrim(st, remst)
Trim(ReplaceString(st, remst,""),"")
EndMacro
a$ = "<>hello<>"
Debug myTrim(a$, "<>")
General remark: I can't see the sense in ignoring existing solutions, that have been posted previously in a thread.
Re: Trim() enhancement
Easy Little John. ;-DLittle John wrote:No. As Shield already indicated, that's not a Trim() function.DK_PETER wrote:@PB
You could do this:
Macro myTrim(st, remst)
Trim(ReplaceString(st, remst,"")," ")
EndMacro
a$ = "<>hello<>"
Debug myTrim(a$, "<>")
General remark: I can't see the sense in ignoring existing solutions, that have been posted previously in a thread.
I completely agree, but in the spirit of a New Year - I provided a non-serious option.
It does however provide the desired result for Shield's "<>he<>llo<>".
Happy New Year!
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
Re: Trim() enhancement
Thank you for your input IdeasVacuum and Little John.
Trim("<>hello<>","<>") would return "hello"
Trim("<>he<>llo<>","<>") would return "he<>llo"
Trim("<<>><<>he<>>>>llo<>>>>>>","<>") would return "he<>>>>llo"
Trim(">hello","<>") would return "hello"
Hope it's clear now.
PB, I'm proposing the function work exactly like Trim, but removing characters from the start and end of a string until it hits one not in the 'characters to remove' string. So:PB wrote:> This would be more powerful
But what would it do to this?
Code: Select all
a$=Trim("<>hello<>","<>")
Trim("<>hello<>","<>") would return "hello"
Trim("<>he<>llo<>","<>") would return "he<>llo"
Trim("<<>><<>he<>>>>llo<>>>>>>","<>") would return "he<>>>>llo"
Trim(">hello","<>") would return "hello"
Hope it's clear now.
Re: Trim() enhancement
Yes, it is clear now.paulr wrote:PB, I'm proposing the function work exactly like Trim, but removing characters from the start and end of a string until it hits one not in the 'characters to remove' string. So:
Trim("<>hello<>","<>") would return "hello"
Trim("<>he<>llo<>","<>") would return "he<>llo"
Trim("<<>><<>he<>>>>llo<>>>>>>","<>") would return "he<>>>>llo"
Trim(">hello","<>") would return "hello"
Hope it's clear now.
Code: Select all
Procedure.s TrimAll(inputString.s, characters.s)
Protected i, *p.Character
Protected len = Len(inputString)
If len > 0
len - 1
For i = 0 To len
*p = @characters
While *p\c <> 0
inputString = Trim(inputString,Chr(*p\c))
*p + SizeOf(Character)
Wend
Next
EndIf
;Debug "TrimAll Iterations: "+Str(i+1)
ProcedureReturn inputString
EndProcedure
Macro Trim(string,chars=" ") : TrimAll(string,chars) : EndMacro
Debug Trim("<>hello<>","<>") ; would return "hello"
Debug Trim("<>he<>llo<>","<>") ; would return "he<>llo"
Debug Trim("<<>><<>he<>>>>llo<>>>>>>","<>") ; would return "he<>>>>llo"
Debug Trim(">hello","<>") ; would return "hello"
Debug Trim("<hello>","<>o") ; would return "hell"
Debug Trim("<hello>","<o>") ; would return "hell"
Debug Trim(" hello ") ; would return "hello"
Code: Select all
Procedure.s TrimAll(inputString.s, characters.s)
Protected i, *p.Character, newString.s, foundChange
Protected len = Len(inputString)
If len > 0
len - 1
For i = 0 To len
*p = @characters
foundChange = #False
While *p\c <> 0
newString = Trim(inputString,Chr(*p\c))
If newString <> inputString
foundChange = #True
EndIf
inputString = newString
*p + SizeOf(Character)
Wend
If foundChange = #False
Break
EndIf
Next
EndIf
;Debug "TrimAll Iterations: "+Str(i+1)
ProcedureReturn inputString
EndProcedure
Macro Trim(string,chars=" ") : TrimAll(string,chars) : EndMacro
Debug Trim("<>hello<>","<>") ; would return "hello"
Debug Trim("<>he<>llo<>","<>") ; would return "he<>llo"
Debug Trim("<<>><<>he<>>>>llo<>>>>>>","<>") ; would return "he<>>>>llo"
Debug Trim(">hello","<>") ; would return "hello"
Debug Trim("<hello>","<>o") ; would return "hell"
Debug Trim("<hello>","<o>") ; would return "hell"
Debug Trim(" hello ") ; would return "hello"
Re: Trim() enhancement
Thanks, that's a very neat implementation. I'd like to see that in the next version of PB!
- Andre
- PureBasic Team
- Posts: 2137
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: Trim() enhancement
+1paulr wrote:Thanks, that's a very neat implementation. I'd like to see that in the next version of PB!

Re: Trim() enhancement
Code: Select all
Procedure.s TrimAll(in.s, what.s)
While FindString(what, Left(in, 1)) Or FindString(what, Right(in, 1))
For i=1 To Len(what)
in=Trim(in, Mid(what, i, 1))
Next
Wend
ProcedureReturn in
EndProcedure
Debug TrimAll("<>Hello<>", "<>")
Debug TrimAll("<>Hel<>lo<>", "<>")
Debug TrimAll("<>Hello<>", "<o>")
Debug TrimAll("<>Hello<>", "o><")
Debug TrimAll("<>Hello<>", "o<>")
Debug TrimAll("<>Hello<>", "<>o")
Debug TrimAll("<><.>><<>Hel<>lo<>:<>>::<><<>>", "<:>.")
Code: Select all
Procedure.s TrimAll(in.s, what.s)
While FindString(what, Left(in, 1))
in=Trim(in, Left(in,1))
Wend
While FindString(what, Right(in, 1))
in=RTrim(in, Right(in, 1))
Wend
ProcedureReturn in
EndProcedure
Debug TrimAll("<>Hello<>", "<>")
Debug TrimAll("<>Hel<>lo<>", "<>")
Debug TrimAll("<>Hello<>", "<o>")
Debug TrimAll("<>Hello<>", "o><")
Debug TrimAll("<>Hello<>", "o<>")
Debug TrimAll("<>Hello<>", "<>o")
Debug TrimAll("<><.>><<>Hel<>lo<>:<>>::<><<>>", "<:>.")
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg