在 Windows 命令提示符中,我尝试替换中的空格set string=Hello World
用字符串%20
。天真地尝试像这样使用字符串文字 %20 :
set string=%string: =%20%
结果是HelloWorld20%
。尝试使用转义字符^
像这样:
set string=%string: =^%20%
也导致HelloWorld20%
。试图逃离%
像这样加倍:
set string=%string: =%%20%
结果是HelloWorld%20%
。我还尝试使用另一个变量来进行替换,如下所示:
set r=%20
set string=%string: =%r%%
这导致HelloWorldr%%
.
I found this https://stackoverflow.com/questions/14718591/escape-percent-signs-in-given-variables,它处理变量中百分比字符的转义。我还发现this https://stackoverflow.com/questions/10296162/escaping-special-characters-in-cmd,它处理百分比字符的转义输入。但这似乎都不适用于字符串替换。
A Windows cmd.exe 的教程/文档页面 http://ss64.com/nt/syntax-replace.html我在网上找到的告诉我我有正确的语法,但不包括用百分比字符替换。
读完所有这些后,我尝试了:
setlocal EnableDelayedExpansion
set string=!string: =%20!
这导致!string: =%20!
.
我没有主意,你能帮忙吗?