| View previous topic :: View next topic |
| Author |
Message |
Dr. Dread Professional Member


Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Thu Sep 19, 2002 6:28 pm Post subject: String processing - whitespace chars find/remove |
|
|
Does what the heading says - list the whitespace characters found in a string.
Greetz
Dr. Dread
| Code: |
%%inc = 0
list create,1
repeat
%%string = This is@tab()just a test for whitespace@cr()
%%dummy = ""
%%list = ""
%%inc = @succ(%%inc)
gosub cycle%%inc
repeat
%%pos = @pos(%%char,%%string)
if @greater(%%pos,0)
%%dummy = %%dummy@substr(%%string,1,%%pos)
%%string = @strdel(%%string,1,%%pos)
%%list = %%list@len(%%dummy)+
end
until @equal(%%pos,0)
if @not(@null(%%dummy))
list add,1,%%name found at position @substr(%%list,1,@pred(@len(%%list)))
end
REM If you don't want to include space then use 'until @equal(%%inc,5)'
until @equal(%%inc,6)
info @text(1)
stop
REM ##### SUBs #####
:cycle1
%%name = Horizontal tab
%%char = @CHR(9)
EXIT
:cycle2
%%name = New line
%%char = @CHR(10)
EXIT
:cycle3
%%name = Vertical tab
%%char = @CHR(11)
EXIT
:cycle4
%%name = Form feed
%%char = @CHR(12)
EXIT
:cycle5
%%name = Carriage return
%%char = @CHR(13)
EXIT
:cycle6
%%name = Space character
%%char = @CHR(32)
EXIT
|
_________________ ~~ 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:27 am; edited 2 times in total |
|
| Back to top |
|
 |
Dr. Dread Professional Member


Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Thu Sep 19, 2002 6:34 pm Post subject: |
|
|
And here we can remove those buggers. All whitespace characters are removed, except spaces - but if contiguous spaces are found,
they are compressed into a single blank.
Greetz
Dr. Dread
| Code: |
REM The question is here: What do we define as whitespace characters?
REM Any contiguous sequence of tabs, carriage returns, form feeds, and/or line feeds are.
REM Spaces are tricky - basically they are whitespace, but normally you wouldn't want to
REM remove every space in your string - generally it will make more sense to compress space
REM sequences into a single space. That's what's done here.
%%string = " This is a test "@tab()" string. And here this line ends."@cr()@tab()@tab()" And ANOTHER line. "
REM Remove leading/trailing normal whitespace
%%string = @trim(%%string)
rem Compress spaces to just 1
if @greater(@pos(" ",%%string),0)
repeat
%%pos = @pos(" ",%%string)
%%string = @strdel(%%string,%%pos,)
until @equal(@pos(" ",%%string),0)
end
%%len = @len(%%string)
rem Cycle through the string, char by char
rem When found, remove whitespace chars
%%inc = 0
repeat
%%inc = @succ(%%inc)
%%dummy = @substr(%%string,%%inc)
if @equal(%%dummy,@CHR(9))@equal(%%dummy,@CHR(10))@equal(%%dummy,@CHR(11))@equal(%%dummy,@CHR(12))@equal(%%dummy,@CHR(13))
%%dummy = ""
end
%%newstring = %%newstring%%dummy
until @equal(%%inc,%%len)
info @chr(34)%%newstring@chr(34)
|
_________________ ~~ 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
|
|