[英]How to replace with percent character in cmd.exe?
In the Windows command prompt, I am trying to replace the space in set string=Hello World
with the string %20
. 在Windows命令提示符中,我试图用字符串%20
替换set string=Hello World
中的空格。 Naively trying to use the string literal %20 like this: 天真地尝试使用字符串文字%20,如下所示:
set string=%string: =%20%
results in HelloWorld20%
. 结果是HelloWorld20%
。 Trying to use the escape character ^
like this: 试图使用转义字符^
像这样:
set string=%string: =^%20%
also results in HelloWorld20%
. 也导致HelloWorld20%
。 Trying to escape the %
by doubling it like this: 试图通过将它加倍来逃避%
:
set string=%string: =%%20%
results in HelloWorld%20%
. 结果是HelloWorld%20%
。 I also tried to use another variable to do the replacement like this: 我还尝试使用另一个变量进行替换,如下所示:
set r=%20
set string=%string: =%r%%
which results in HelloWorldr%%
. 这导致HelloWorldr%%
。
I found this , which handles the escaping of percent characters in variables. 我找到了这个 ,它处理变量中百分比字符的转义。 I also found this , which handles the escaped input of percent characters. 我也找到了这个 ,它处理百分比字符的转义输入。 But neither one seems to apply to string replacing. 但似乎没有人适用于字符串替换。
A tutorial/docu page for the Windows cmd.exe which I found online tells me I have the correct syntax, but does not cover replacing with percent characters. 我在网上找到的Windows cmd.exe的教程/文档页面告诉我我的语法正确,但不包括用百分号字符替换。
After reading all of those, I tried: 阅读完所有内容后,我尝试了:
setlocal EnableDelayedExpansion
set string=!string: =%20!
which results in !string: =%20!
结果是!string: =%20!
. 。
I am out of ideas, can you help? 我没有想法,你能帮忙吗?
You need delayed expansion
in this case: 在这种情况下,您需要delayed expansion
:
set "str1=Hello World!"
set "str2=%20"
for /f "delims=" %a in ('cmd /v:on /c @echo "%str1: =!str2!%"') do set "str3=%~a"
echo %str3%
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.