Hi All!
Below is an alternative technique for trimming leading and trailing spaces and tabs from a string…
Edit: Please read revised code.
- SB
strimmer: a string trimmer
Moderator: DosItHelp
-
- Posts: 231
- Joined: 01 Oct 2012 13:32
- Location: Ireland
- Contact:
strimmer: a string trimmer
Last edited by Sponge Belly on 13 Nov 2017 09:36, edited 1 time in total.
Re: strimmer: a string trimmer
The use of the Escape and Backspace character prevents from trimming:
The output then is:
penpen
Edit: Sorry the example was bad as it worked as expected, i have corrected it.
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% "
Code: Select all
7: abc ]
8:[ abc ]
penpen
Edit: Sorry the example was bad as it worked as expected, i have corrected it.
Last edited by penpen on 07 Aug 2013 13:07, edited 1 time in total.
-
- Posts: 231
- Joined: 01 Oct 2012 13:32
- Location: Ireland
- Contact:
Re: strimmer: a string trimmer
Last edited by Sponge Belly on 13 Nov 2017 09:39, edited 2 times in total.
Re: strimmer: a string trimmer
Hi Sponge Belly,
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
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
-
- Posts: 231
- Joined: 01 Oct 2012 13:32
- Location: Ireland
- Contact:
Re: strimmer: a string trimmer
Thanks for the explanation, Jeb!
- SB
- SB
Last edited by Sponge Belly on 13 Nov 2017 09:40, edited 1 time in total.
-
- Posts: 231
- Joined: 01 Oct 2012 13:32
- Location: Ireland
- Contact:
Re: strimmer: a string trimmer
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!
Thanks to Jeb for his awesome safe return technique and Dave Benham’s tip on how to avoid caret-doubling when using CALL.
- SB
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
- SB