Copy Code code as follows:
@echo off
:: Remove left space
::
::
Set "Str= ab c&>!"
for/f "tokens=*"%%i in ("%str%") do echo "☆%%i☆"
Pause
Goto:eof
@echo off
:: Remove 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 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 trailing space space
::
::
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 space spaces
::
::
Set "Str= ab c&>!"
Set "STR=%STR: =%"
echo "☆%str%☆"
Pause
Goto:eof
@echo%dbg% off
CLS && Echo. && Echo.
: Redtek 2006 to left and right contain space variable test and alternative method implementation Demo
:: In order to demonstrate and do it with convenience, so "tag code snippet" code can be used independently
::
Set "Var= My name is Redtek"
REM Note: The parameters in call below are enclosed in quotes, with parameters separated by commas, spaces, etc.
Call: Go to the left space%var%
Echo. && Echo.
Call: Go to the right space "%var%"
REM goes to the left and right spaces in the string
REM for demonstration and other convenience, the following steps will be rewritten to the left and right spaces of the code snippet, can be used independently.
Echo. && Echo.
Call: Go to both sides of the space "%var%"%var%
Goto:eof
: Go to the left space
REM replaces everything in the variable var with the left of the ^%1 variable content with the ^1 itself, using the substitution principle.
When REM is called, the spaces in the parameters are all "lost" (using the attributes separated by the parameters),
REM So, naturally, the space on the left of the ^%1 to be positioned is all gone.
Call set "to the left space after the variable =%%var:*%1=%1%%
echo Original string: [%var%]
Echo goes to the left space after the string: [% to left space after the variable%]
Goto:eof
: Go to the right space
REM assigns the result to a variable, which can be arbitrarily invoked.
REM ~NX uses the "extend variable to a filename" feature,
REM since it is a legal file name, of course, the space behind the filename is invalid and useless ~:)
Set to the right space after the variable =%~nx1
echo Original string: [%var%]
Echo goes to the right space after the string: [% to the right space after the variable%]
Goto:eof
: Go to both sides of the space
REM principle: First to the left space, and then to the right space, equivalent to the above demo code sharing:
REM in the case of the code without the above two left and right spaces, the following standalone demo:
Set to the left and right space after the variable =%~nx1
Call set "goes to the left and right space after the variable =%% to the left and right space after the variable: *%2=%2%%"
echo Original string: [%var%]
Echo goes to the left and right space after the string: [% to the left and right spaces after the variable%]
Goto:eof