Page 1 of 1
Trim() enhancement
Posted: Mon Dec 30, 2013 4:59 pm
by paulr
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.
Re: Trim() enhancement
Posted: Mon Dec 30, 2013 5:12 pm
by IdeasVacuum
Re: Trim() enhancement
Posted: Mon Dec 30, 2013 5:27 pm
by Little John
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?
Yes, that would be very useful. E.g. PowerBasic has this feature for ages.
( Until this will be implemented in PureBasic, you can use self-written functions for this. )
Re: Trim() enhancement
Posted: Tue Dec 31, 2013 12:36 pm
by PB
> This would be more powerful
But what would it do to this?
Re: Trim() enhancement
Posted: Tue Dec 31, 2013 1:17 pm
by DK_PETER
PB wrote:> This would be more powerful
But what would it do to this?
@PB
You could do this:
Macro myTrim(st, remst)
Trim(ReplaceString(st, remst,""),"")
EndMacro
a$ = "<>hello<>"
Debug myTrim(a$, "<>")
Re: Trim() enhancement
Posted: Tue Dec 31, 2013 1:23 pm
by Shield
What if the string is "<>he<>llo<>"?

Re: Trim() enhancement
Posted: Tue Dec 31, 2013 6:38 pm
by Little John
PB wrote:> This would be more powerful
But what would it do to this?
See the link in the post directly above your post ...
Re: Trim() enhancement
Posted: Tue Dec 31, 2013 6:45 pm
by Little John
DK_PETER wrote:@PB
You could do this:
Macro myTrim(st, remst)
Trim(ReplaceString(st, remst,""),"")
EndMacro
a$ = "<>hello<>"
Debug myTrim(a$, "<>")
No. As Shield already indicated, that's not a Trim() function.
General remark: I can't see the sense in ignoring existing solutions, that have been posted previously in a thread.
Re: Trim() enhancement
Posted: Tue Dec 31, 2013 8:29 pm
by DK_PETER
Little John wrote:DK_PETER wrote:@PB
You could do this:
Macro myTrim(st, remst)
Trim(ReplaceString(st, remst,"")," ")
EndMacro
a$ = "<>hello<>"
Debug myTrim(a$, "<>")
No. As Shield already indicated, that's not a Trim() function.
General remark: I can't see the sense in ignoring existing solutions, that have been posted previously in a thread.
Easy Little John. ;-D
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!
Re: Trim() enhancement
Posted: Fri Jan 03, 2014 11:19 am
by paulr
Thank you for your input IdeasVacuum and Little John.
PB wrote:> This would be more powerful
But what would it do to this?
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.
Re: Trim() enhancement
Posted: Fri Jan 03, 2014 4:58 pm
by Danilo
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.
Yes, it is 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"
The following version trims only until no more changes are found (better for large strings):
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
Posted: Sat Jan 04, 2014 11:44 am
by paulr
Thanks, that's a very neat implementation. I'd like to see that in the next version of PB!
Re: Trim() enhancement
Posted: Sat Jan 04, 2014 9:18 pm
by Andre
paulr wrote:Thanks, that's a very neat implementation. I'd like to see that in the next version of PB!
+1

Re: Trim() enhancement
Posted: Sun Jan 05, 2014 1:38 am
by Thade
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<>:<>>::<><<>>", "<:>.")
or this does the same
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<>:<>>::<><<>>", "<:>.")