对不起,我的措辞首先不知道怎么说这个。我的问题是我有一个带有2个变量的循环,我想简单地检查是否存在多个字符的示例代码是:
if %time:~8,1% EQU "" echo Less than 8 characters
我头脑中的代码(这是错误的)看起来像这样
set "AN1=Little String"
set "AN2=Along String More Character"
set "AN3=Extra String With A Lot Character"
set "AN3=SmallSt"
for /l %%A IN (1,1,28) DO (
If Not !AN%%!:~8,1 EQU "" (echo String has Less than 8 characters) ELSE (
If Not !AN%%!:~16,1 EQU "" (echo String has Less than 16 characters) ELSE (
If Not !AN%%!:~24,1 EQU "" (echo String has Less than 24 characters) ELSE (
)
)
答案 0 :(得分:1)
您希望根据长度阈值测试字符串的“数组”。你的逻辑是倒置的(如果第8个字符不为空,那么字符串短于8:错误)除了它已经接近。
关键是:set V=!AN%%A%!
。这允许从另一个“生成”变量名称。 EnableDelayedExpansion
部分对于能够使用除%
之外的其他分隔符非常有用:!
否则嵌套会失败。
我采用了5个字符串的例子来涵盖所有测试。
@echo off
setlocal EnableDelayedExpansion
set "AN1=Little String"
set "AN2=Along String More Character"
set "AN3=Extra String With A Lot Character"
set "AN4=SmallSt"
set "AN5=Along String More "
for /l %%A IN (1,1,5) DO (
set V=!AN%%A%!
If "!V:~8,1!"=="" (echo "!V!" String has Less than 8 characters) else (
If "!V:~16,1!"=="" (echo "!V!" String has Less than 16 characters) else (
If "!V:~24,1!"=="" (echo "!V!" String has Less than 24 characters)
)
)
)
结果:
"Little String" String has Less than 16 characters
"SmallSt" String has Less than 8 characters
"Along String More " String has Less than 24 characters