我有一个复杂的字符串,我正在尝试解析。 我有一个简短的子程序来细化两个特定字符的索引,我需要在它们之间的字符串。 所以问题是:如何在另一个变量的字符串操作中使用变量?
这是一些示例代码:
@ECHO off set start=2 set end=5 set str=Hello World echo %str:~2,5% echo %str:~%start%,%end%%如何获取第二个回波以显示第一个回波显示的内容? (现在第二个回声只显示我想要的东西,它会崩溃)
I have a complex string I'm trying to parse. I have a short subroutine to fine the index of two specific characters, and I need the string in between. So the question is: How do I use variables inside string manipulation of another variable?
Here's some sample code:
@ECHO off set start=2 set end=5 set str=Hello World echo %str:~2,5% echo %str:~%start%,%end%%How do I get the second echo to display what the first echo displays? (right now second echo is just showing what I want, it'll crash as is)
最满意答案
@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 @ECHO off set start=2 set end=5 set str=Hello World setlocal enableDelayedExpansion echo %str:~2,5% echo !str:~%start%,%end%! endlocalor (the worst way)
@ECHO off set start=2 set end=5 set str=Hello World echo %str:~2,5% call echo %%str:~%start%,%end%%%or (can be used if start and end definition and the substringing are all in brackets context)
@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! endlocalor (also a bad way)
@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更多推荐
发布评论