批处理文件:是否可以提取索引为变量的子字符串?

And*_*rew 2 windows substring batch-file

如果我有两个变量,string并且index,我想从索引中提取一个子字符串,我可以使用该SET命令来执行此操作吗?例如:

@ECHO off
SET string=Hello
SET index=3
ECHO %string:~%index%%
Run Code Online (Sandbox Code Playgroud)

Helloindex%当预期结果为时,返回lo.我正在尝试做什么?

问候,

安德鲁

Mag*_*goo 5

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET string=Hello
SET index=3
ECHO !string:~%index%!
GOTO :EOF
Run Code Online (Sandbox Code Playgroud)

当然.这是使用delayedexpansion的一种方法.

您可能使用哪种方法取决于您的意图和参数.对于众多解决方案和特殊情况,您的问题实在太普遍了.