Batch string to space implementation code [different parts]

Time:2021-1-27
Copy codeThe code is as follows:
@echo off
:: remove the left space
::
::
set “str= ab c&>! ”
for /f “tokens=*” %%i in (“%str%”) do echo “☆%%i☆”
pause
goto :eof

@echo off
:: remove the left space
::
::
set “str= ab c&>! ”
:intercept
if “%str:~0,1%”==” ” set “str=%str:~1%”&goto intercept
echo “☆%str%☆”
pause
goto :eof

@echo off
:: remove the right space
::
::
set “str= ab c&>! ”
:intercept
if “%str:~-1%”==” ” set “str=%str:~0,-1%”&goto intercept
echo “☆%str%☆”
pause
goto :eof

@echo off
:: remove the leading and trailing spaces
::
::
set “str= ab c&>! ”

:intercept_left
if “%str:~0,1%”==” ” set “str=%str:~1%”&goto intercept_left

:intercept_right
if “%str:~-1%”==” ” set “str=%str:~0,-1%”&goto intercept_right
echo “☆%str%☆”
pause
goto :eof

@echo off
:: remove all spaces
::
::
set “str= ab c&>! ”
set “str=%str: =%”
echo “☆%str%☆”
pause
goto :eof

@echo %dbg% off
cls && echo. && echo.
: redtek 2006 experiment of removing left and right variables with spaces and demonstration of alternative methods
: for the convenience of demonstration and doing it separately, the code in the “tag code segment” can be used independently
::

set “var= My name is Redtek ”
REM note: the parameters in the following call are without quotation marks, and are separated by commas and spaces
Call: remove the left space% var%
echo. && echo.
Call: go to the right space% var% “
REM to remove the left and right spaces in the string
REM for the convenience of demonstration and other use, the following steps will rewrite the code segment to the left and right spaces, which can be used independently.
echo. && echo.
Call: remove both sides of the space% var% “% var%
goto :eof

: to the left

REM uses the replacement principle to replace everything on the left side of the string that starts with the content of ^% 1 variable in VaR with ^ 1 itself
Rem is called because all the spaces in the parameter are “lost” (using the property of parameter separation),
REM therefore, naturally, the spaces to the left of the ^% 1 to be located are all gone.

Call set “variable after removing the left space =%% var: *% 1 =%”

Echo original string: [% var%]
Echo string with left space removed: [% variable with left space removed%]
goto :eof

: to the right

REM assigns the result to a variable, which can be called at will.
REM ~ NX takes advantage of the feature of “extending variables to a file name”,
Since rem is a legal file name, the space after the file name is invalid and useless

Set variables after right space =% – NX1

Echo original string: [% var%]
Echo string after right space: [% variable after right space%]
goto :eof

: remove the spaces on both sides

REM principle: first to the left space, then to the right space, equivalent to the above demonstration code:)
REM in the absence of the above two paragraphs to the left and right space code, the following independent demonstration:

Set variables after left and right spaces =% – NX1
Call set “variables after left and right spaces =%% variables after left and right spaces: *% 2 =%”

Echo original string: [% var%]
Echo string after left and right spaces: [% variable after left and right spaces%]
goto :eof