@ECHO off
set start=2
set end=5
set str=Hello World
setlocal enableDelayedExpansion
echo %str:~2,5%
echo !str:~%start%,%end%!
endlocal
或(最糟糕的方式)
@ECHO off
set start=2
set end=5
set str=Hello World
echo %str:~2,5%
call echo %%str:~%start%,%end%%%
或(可以使用,如果起點和終點的定義和該substringing都在括號上下文)
@ECHO off
set start=2
set end=5
set str=Hello World
echo %str:~2,5%
setlocal enableDelayedExpansion
for /f "tokens=1,2" %%a in ("%start% %end%") do echo !str:~%%a,%%b!
endlocal
或(也是個不錯的方法)
@ECHO off
set start=2
set end=5
set str=Hello World
call :substr "%str%" %start% %end%
goto :eof
:substr
setlocal enableDelayedExpansion
set "_str=%~1"
echo !_str:~%2,%3!
rem without delayedExpansion
rem call echo %%_str:~%2,%3%%
endlocal
goto :eof
老鼠,我甚至嘗試過這兩種。但在第一種情況下,我忘記啓用延遲擴展,而在第二種情況下,我忘記了最後一個%。感謝您及時的回覆! – user2370851