我需要将来自 for 循环的变量临时保存在 %%c 中。但当我尝试这样做时,内容却出乎意料地发生了变化。字符串末尾出现一些空格字符。顺便说一句,%%c 的内容是 a.jpg。
echo %%ca REM prints a.jpga
set temp=%%c
set temp=!temp!
echo !temp!a REM prints a.jpg a
我尝试使用下面的代码来在初始化临时变量后删除多余的空格。但它给了我一个错误:“=%”此时是意外的”。我错过了什么?提前感谢!
set "this=!temp!"
set "this=%this:* =%"
call set "this=%%temp:%this%=%%"
set "this=%this:~0,-1%"
echo %this%a
最佳答案
你的行set temp=%%c
就是原因。末尾有空格。
使用此语法可以避免意外的空格:
set "temp=%%c"
关于windows - 使用批处理脚本进行字符串操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006553/