Я пытался создать простой командный файл, который сканирует переменную X на наличие пробелов, а затем создает измененную переменную Xmodified, не содержащую пробелов. Вот код, который я использовал:
@echo off установить Xmodified =[
set "X=I am good"
:space1
IF "%X:~0,1%" == " " ( goto space2 ) Else ( set Xmodified=%Xmodified%%X:~0,1% )
goto space2
:space2
IF "%X:~1,1%" == " " ( goto space3 ) Else ( set Xmodified=%Xmodified%%X:~1,1% )
goto space3
:space3
IF "%X:~2,1%" == " " ( goto space4 ) Else ( set Xmodified=%Xmodified%%X:~2,1% )
goto space4
:space4
IF "%X:~3,1%" == " " ( goto space5 ) Else ( set Xmodified=%Xmodified%%X:~3,1% )
goto space5
:space5
IF "%X:~4,1%" == " " ( goto space6 ) Else ( set Xmodified=%Xmodified%%X:~4,1% )
goto space6
:space6
IF "%X:~5,1%" == " " ( goto space7 ) Else ( set Xmodified=%Xmodified%%X:~5,1% )
goto space7
:space7
IF "%X:~6,1%" == " " ( goto space8 ) Else ( set Xmodified=%Xmodified%%X:~6,1% )
goto space8
:space8
IF "%X:~7,1%" == " " ( goto space9 ) Else ( set Xmodified=%Xmodified%%X:~7,1% )
goto space9
:space9
IF "%X:~8,1%" == " " ( goto Finishing ) Else ( set Xmodified=%Xmodified%%X:~8,1% )
goto Finishing
:Finishing
set Xmodified=%Xmodified:~1%
goto Display
:Display
cls
echo Your Processed Result:%Xmodified%
pause >nul
Output:
Your Processed Result: I a m g o o d
Can someone tell me why the output includes spaces after every letter? From where did these spaces come?
How do I make the output like this?
Your Processed Result:Iamgood
Please help. Thank you.
1 Answer
How do I remove spaces from a variable?
If you are going to use for
or if
(which have expressions using brackets) then you would need to use Delayed Expansion.
However there is a much easier solution that removes the spaces all at once:
@echo off
setlocal
set "X=I am good"
set _result=%X: =%
echo %_result%
endlocal