gpt4 book ai didi

batch-file - 批处理文件 : Variable within a variable

转载 作者:行者123 更新时间:2023-12-04 21:37:48 26 4
gpt4 key购买 nike

我要解析一个复杂的字符串。我有一个简短的子程序来确定两个特定字符的索引,我需要介于两者之间的字符串。所以问题是:如何在另一个变量的字符串操作中使用变量?

下面是一些示例代码:

@ECHO off

set start=2
set end=5
set str=Hello World

echo %str:~2,5%
echo %str:~%start%,%end%%

如何让第二个回显显示第一个回显显示的内容? (现在第二个回声只是显示我想要的,它会按原样崩溃)

最佳答案

@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%%%

或(如果开始和结束定义以及子字符串都在括号上下文中,则可以使用)

@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

关于batch-file - 批处理文件 : Variable within a variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19644935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com