windows - 使用批处理脚本进行字符串操作

标签 windows string batch-file space

我需要将来自 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/

相关文章:

windows - 如何在批处理文件中用空格分隔嵌入空格的双引号字符串?

Windows CMD 删除目录

windows - 如何在 Windows XP 中监视 UDP 缓冲区

c - 如何读取存档文件格式(Rar/Zip)内的文本文件而不仅使用 'C' 提取存档文件?

python - 将字典转换为字符串python

Ruby 字符串到运算符

Windows批处理文件获取C :\drive total space and free space available

ruby - 是否可以在 ruby​​/tk 中使用 unicode 字符?

php - 使用 MYSQL PHP 按每个单词而不是完整的字符串搜索

cmd - bat 文件: get parent pathname