当左手侧为双引号时,如何在批处理脚本中比较字符
我有Windows批处理代码,我正在尝试测试Value的第一个字符是否是连字符,但是当Value的第一个字符是双引号时,代码会失败。 (值来自真实代码的其他位置。)
set value=""This is a test""
set arg="%value%"
set prefix=%arg:~1,1%
if "%prefix%" == "-" ...
评估
如果“”“ = =” - “
,并且生成命令的语法不正确。我尝试插入脑图进入平等检查的两侧
if "^%prefix%" == "^-" ...
,但会产生相同的错误
if "^"" == "^-"
I have Windows Batch code where I'm trying to test if the first character of value is a hyphen but the code fails when the first character of value is a double quote. (value is coming from elsewhere in the real code.)
set value=""This is a test""
set arg="%value%"
set prefix=%arg:~1,1%
if "%prefix%" == "-" ...
Evaluates to
if """ == "-"
and generates The syntax of the command is incorrect. I tried inserting a caret into both sides of the equality check
if "^%prefix%" == "^-" ...
but that generates the same error with
if "^"" == "^-"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用Caret逃避单个角色,例如
诀窍是逃避报价,否则内在的商人没有特殊的含义。
这不能适用于一个以上的字符,但是您可以切换到简单的延迟扩展。它的扩展总是安全的。
You can use the caret to escape a single character, like
The trick is to escape the quotes, too, else the inner caret has no special meaning.
This can't work for more than one character, but you could switch to the even simple delayed expansion. It's expansion is always safe.
测试值的第一个字符是否是连字符
另一种方法(使用原始字符串,没有其他变量):
test if the first character of value is a hyphen
Another approach (using the original string, no additional variables):