批处理脚本如何通过变量索引获取子字符串
How batch script get substring by variable index
我知道我们可以在批处理脚本中得到这样的子字符串:
SET a=abcdefgh
ECHO %a:~3,2%
但是如何通过可变索引获取字母呢?有点像:
SET index=3
ECHO %a:~%index%,1%
由于子字符串操作需要一个常量,因此必须在子进程中调用它,这时 CALL 就派上用场了:
SET _index=3
call set b=%%a:~%index%,1%%
echo (%b%)
有关详细信息,请参阅 https://ss64.com/nt/syntax-substring.html。 SS64/nt 是任何与批处理相关的优秀资源。
我知道我们可以在批处理脚本中得到这样的子字符串:
SET a=abcdefgh
ECHO %a:~3,2%
但是如何通过可变索引获取字母呢?有点像:
SET index=3
ECHO %a:~%index%,1%
由于子字符串操作需要一个常量,因此必须在子进程中调用它,这时 CALL 就派上用场了:
SET _index=3
call set b=%%a:~%index%,1%%
echo (%b%)
有关详细信息,请参阅 https://ss64.com/nt/syntax-substring.html。 SS64/nt 是任何与批处理相关的优秀资源。