View previous topic :: View next topic |
Author |
Message |
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Wed Sep 04, 2002 8:04 am Post subject: String manipulation - Trimleft & Trimright |
|
|
Just two small routines removing plain whitespace chars (spaces and tabs) from just one side of a string - as opposed
to VDS @trim() which will always do both.
Code: |
%%string = " some string "
REM You may add in some tabs and spaces to the left and right in the above string
REM Forum just shows a single space
REM ######## Trimleft routine ########
%%outstring = %%string
%%inc = 0
repeat
%%inc = @succ(%%inc)
%%char = @substr(%%string,%%inc,)
REM Check for spaces and tabs
if @equal(%%char," ")@equal(%%char,@tab())
%%outstring = @strdel(%%outstring,1,)
else
REM The first time a non-whitespace char is met, we exit the loop
%%inc = @len(%%string)
end
until @equal(%%inc,@len(%%string))
REM ######## Trimright routine ########
%%outstring2 = %%string
%%inc = @len(%%string)
repeat
%%char = @substr(%%string,%%inc,)
REM Check for spaces and tabs
if @equal(%%char," ")@equal(%%char,@tab())
%%outstring2 = @strdel(%%outstring2,@len(%%outstring2),)
else
REM The first time a non-whitespace char is met, we exit the loop
%%inc = 1
end
%%inc = @pred(%%inc)
until @equal(%%inc,0)
info input@TAB()= @CHR(34)%%string@CHR(34)@CR()trimleft@TAB()= @CHR(34)%%outstring@CHR(34)@CR()trimright@TAB()= @CHR(34)%%outstring2@CHR(34)
|
Greetz
Dr. Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing
Last edited by Dr. Dread on Tue Jul 29, 2003 11:30 am; edited 3 times in total |
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Wed Sep 04, 2002 9:15 am Post subject: |
|
|
Or ya can do it this way with no other vars...
chr(32) is space, @chr(9) is tab
__________________________________________________________________________________________________________________________
Code: |
%s = @chr(9)@chr(32)Test string@chr(32)@chr(9)
INFO String:@cr()@cr()@chr(34)%s@chr(34)
rem -- Remove spaces/tabs from left --
REPEAT
if @equal(@substr(%s, 1), @chr(9))@equal(@substr(%s, 1), @chr(32))
%s = @strdel(%s, 1)
end
UNTIL @both(@not(@equal(@substr(%s, 1), @chr(9))), @not(@equal(@substr(%s, 1), @chr(32))))
INFO Left spaces/tabs removed:@cr()@cr()@chr(34)%s@chr(34)
rem -- Remove spaces/tabs from right --
REPEAT
if @equal(@substr(%s, @len(%s)), @chr(9))@equal(@substr(%s, @len(%s)), @chr(32))
%s = @strdel(%s, @len(%s))
end
UNTIL @both(@not(@equal(@substr(%s, @len(%s)), @chr(9))), @not(@equal(@substr(%s, @len(%s)), @chr(32))))
INFO Right spaces/tabs removed:@cr()@cr()@chr(34)%s@chr(34)
|
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
|
|
Back to top |
|
|
Tommy Admin Team
Joined: 16 Nov 2002 Posts: 746 Location: The Netherlands
|
Posted: Wed Sep 04, 2002 12:46 pm Post subject: |
|
|
Or even like this:
Code: |
%%trim = " hello "
gosub trimleft
info Left trimmed: @chr(34)%%trim@chr(34)
%%trim = " hello "
gosub trimright
info Right trimmed: @chr(34)%%trim@chr(34)
exit
:trimleft
%%trim = @substr(@trim(@strins(%%trim,@succ(@len(%%trim)),a)),1,-1)
exit
:trimright
%%trim = @strdel(@trim(@strins(%%trim,1,a)),1)
exit
|
|
|
Back to top |
|
|
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Wed Sep 04, 2002 1:10 pm Post subject: |
|
|
That's brilliant, Tommy!
So simple and efficient that I should kick my own shin for not thinking of it.
Oouuch, did it now! Serves me right!
Greetz
Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing |
|
Back to top |
|
|
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Wed Sep 04, 2002 1:21 pm Post subject: |
|
|
And it may be made even simpler
Code: |
:trimleft
%%trim = @substr(@trim(%%trim"X"),1,-1)
exit
:trimright
%%trim = @strdel(@trim(X%%trim),1)
exit
|
Greetz
Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|