批处理文件:变量中的变量(Batch file: Variable within a variable)

编程入门 行业动态 更新时间:2024-10-12 08:25:47
批处理文件:变量中的变量(Batch file: Variable within a variable)

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

这是一些示例代码:

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

or (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! endlocal

or (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

更多推荐

本文发布于:2023-08-05 05:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   批处理文件   Batch   variable   Variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!