Windows 批处理 : Split String to individual characters to variables

标签 windows string variables batch-file

在 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/

相关文章:

windows - 是否可以更改每台计算机的 Eclipse 工作空间路径

regex - 解析输出并获取十六进制颜色

bash - 在 bash heredoc 中使用变量

javascript - for循环中变量名可以由数组值决定吗?

java - 如何将变量递增到字母表中的下一个或上一个字母?

windows - 适用于 Windows 的 Markdown 预览应用程序

windows - Linux/Unix中 "Performance Counters"的概念

windows - mklink 访问被拒绝但资源管理器成功

c++ - 如何将字符串中的多个数字转换为整数

php - 将 IPv4 更改为 IPv6 字符串