Supposing you have a string value in a variable STRING
, how can you remove leading and trailing whitespaces with a single command (line)?
For instance, the
It is not possible to trim both spaces and tabs in a single line, but just for spaces you may use this trick:
@echo off
setlocal EnableDelayedExpansion
set "x= two spaces, trimmed text string, space-space-space "
echo "%x%"
rem The single line:
set "word=%x: =" & (if "!word!" neq "" set "x2=!x2! !word!") & set "word=%" & set "x2=!x2:~1!"
echo "%x2%"