Batch file 是否有一种批量识别数字的方法,或者我必须使用很多操作系统ifs
所以我的问题是我不想用我的程序玩barchoba,问变量1是以1结尾还是以2结尾等等。。。 你能告诉我一个技巧吗,如何让我的程序知道变量的结尾数字 谢谢你的建议Batch file 是否有一种批量识别数字的方法,或者我必须使用很多操作系统ifs,batch-file,cmd,Batch File,Cmd,所以我的问题是我不想用我的程序玩barchoba,问变量1是以1结尾还是以2结尾等等。。。 你能告诉我一个技巧吗,如何让我的程序知道变量的结尾数字 谢谢你的建议 rem Initialize digits just once, at beginning set "digits=0123456789" rem Test if "variable1" ends in any digit if "!digits:%variable1:~-1%=!" neq "%digits%" ( echo
rem Initialize digits just once, at beginning
set "digits=0123456789"
rem Test if "variable1" ends in any digit
if "!digits:%variable1:~-1%=!" neq "%digits%" (
echo Variable1 ends in digit
) else (
echo Variable1 does not end in digit
)
该方法使用variable1:%variable1:~-1%
的最后一个字符,并尝试从digits变量:“!digits:V=!”
中删除它。如果结果与digits变量的原始值不同:“%digits%”
,则该字符实际上是一个数字
请注意,您需要
setlocal EnableDelayedExpansion
,此方法才能工作…因为%variable:~-1%
扩展到%variable1%
的最后一个字符,您可以使用GEq
,(大于或等于)和LEq
,(小于或等于)比较器对其进行测试
@Echo关闭
Rem未定义变量
设置“variable1=”
设置“数字=”
Rem请求变量字符串值
Set/P“variable1=请输入变量字符串值:”
Rem测试变量字符串值是否以数字结尾
如果“%variable1:~-1%”GEq“0”如果“%variable1:~-1%”LEq“9”设置“数字=%variable1:~-1%”
如果定义了数字(Echo variable1以%digit结尾),则Echo variable1不以数字结尾
Rem添加延迟以读取回显消息
超时3>Nul
您的Else
部件不能正常工作,因为它只适用于第二个if
。如果最后一个字符是@
(例如,有许多),则此代码不显示任何内容!如果使用嵌套括号…@Aacini,则有必要对第一部分重复else
部分。谢谢,我编辑了我的答案,使用了稍微不同的方法。如果最后一个字符是双引号,则会出现语法错误。需要一些额外的逃逸。