在 Windows Batch 中,如果我有一个变量(长度可以改变),例如:“hello world!” 是否可以“拆分”变量,使每个字符都是其自己的变量,以便输出如下所示:
t1=h
t2=e
t3=l
etc.
如有任何帮助,我们将不胜感激。
最佳答案
使用此代码:
setlocal EnableDelayedExpansion
set str="hello world^!"
set tempstr=%str%
set count=0
:loop
if defined tempstr (
set tempstr=%tempstr:~1%
set /a count+=1
set /a pos=%count%-1
set t!count!=!str:~%pos%,1!
goto loop
)
:: check the generated variables
set t
要获取字符串中的第 n 个字符,请使用 set char=%str:~n,1%
。
我希望这对您有帮助!
关于Windows 批处理 : Split String to individual characters to variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26576914/