Page 1 of 1
strimmer: a string trimmer
Posted: 03 Aug 2013 16:20
by Sponge Belly
Hi All!
Below is an alternative technique for trimming leading and trailing spaces and tabs from a string…
Edit: Please read
revised code.
- SB
Re: strimmer: a string trimmer
Posted: 06 Aug 2013 16:19
by penpen
The use of the Escape and Backspace character prevents from trimming:
Code: Select all
for /f "delims=rem " %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
for /f "delims=rem " %%a in ('"prompt $E&for %%b in (1) do rem"') do set "ESC=%%a"
set "val7= %BS% abc %BS% "
set "val8= %ESC% abc %ESC% "
The output then is:
penpen
Edit: Sorry the example was bad as it worked as expected, i have corrected it.
Re: strimmer: a string trimmer
Posted: 07 Aug 2013 09:15
by Sponge Belly
Thanks Penpen…
Edit: Please read
revised code.
- SB
Re: strimmer: a string trimmer
Posted: 08 Aug 2013 02:31
by jeb
Hi Sponge Belly,
Sponge Belly wrote:Btw, is it necessary to double all quotes only to put them all back the way they were the line after next? I don’t follow that step.
Sponge Belly wrote:set "str=!str:"=""!"
if not defined nodelay set "str=%str:!=^^^!%" !
set "str=!str:""="!"
Yes it's necessary, as it avoids problems with unquoted special characters.
You can test it with a string like
"&"&set "str=&"^&"
This would fail when expanding with
set "str=%str:!=^^^!%" ! as it results to
set "str=&"
&" !
By doubling all quotes, all parts of the strings are
inside quotes.
jeb
Re: strimmer: a string trimmer
Posted: 10 Aug 2013 15:50
by Sponge Belly
Thanks for the explanation, Jeb!
- SB
Re: strimmer: a string trimmer
Posted: 13 Nov 2017 09:27
by Sponge Belly
Hello Again!
Wow, I can’t believe this topic is over 4 years old!
What an enthusiastic little puppy I was in those days…
Anyways, I’ve revised the code for my string strimmer. It is now completely expansion-insensitive and exploits the option to omit the closing quote in the last parameter of CALL to trim trailing whitespace. It’s also a lot simpler—a sure sign of improvement!
Code: Select all
@echo off & setlocal enableextensions disabledelayedexpansion
(set lf=^
%= BLANK LINE REQUIRED =%
)
set "cr=" & if not defined cr for /f "skip=1" %%C in (
'echo(^|replace ? . /w /u'
) do set "cr=%%C"
set ^"orig= !random! ! ^^! ^^^^! ^"^^ ^&^"^& ^^^" %%os%% ^"
call :strim res1 orig
setlocal enabledelayedexpansion
call :strim res2 orig
echo(orig: [!orig!]
echo(res1: [!res1!]
echo(res2: [!res2!]
endlocal
endlocal & goto :EOF
:strim result= original=
:: trims leading and trailing whitespace from a string
setlocal
set "ddx=!"
setlocal enabledelayedexpansion
set "die=" & if not defined %2 (>&2 echo(var "%2" not defined
set "die=1") else set "str=!%2!"
if not defined die for %%L in ("!lf!") ^
do if "!str!" neq "!str:%%~L=!" (
>&2 echo(var "%2" contains linefeeds & set "die=1")
if not defined die for %%C in ("!cr!") ^
do if "!str!" neq "!str:%%~C=!" (
>&2 echo(var "%2" contains carriage returns & set "die=1")
if not defined die (for /f eol^= %%A in ("!str!") do rem nop
) || (>&2 echo(var "%2" consists entirely of whitespace
set "die=1")
if defined die goto die
set "str=!str:^=^^^^!"
set "str=!str:"=""!"
set "str=%str:!=^^^!%" !
call :_strim "%%str%%
if not defined ddx set "str=!str:^=^^^^!"
if not defined ddx set "str=%str:!=^^^!%" !
set "str=!str:""="!"
for /f tokens^=*^ eol^= %%A in ("!str!") do (
endlocal & endlocal & set "%1=%%A" !)
exit /b 0
:die
endlocal & endlocal & set "%1=" & exit /b 1
:_strim
:: subfunction
set "str=%~1" !
exit /b 0
Thanks to Jeb for his awesome
safe return technique and Dave Benham’s tip on
how to avoid caret-doubling when using CALL.
- SB