Windows批处理:将字符串拆分为单个字符并转换为变量 [英] Windows Batch: Split String to individual characters to variables

查看:78
本文介绍了Windows批处理:将字符串拆分为单个字符并转换为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows Batch中,如果我有一个变量(长度可以更改),例如:"hello world!"是否有可能拆分"变量,以便每个字符都是其自己的变量,因此输出看起来像:

in Windows Batch, if I had a variable (length can change) that was, for example: "hello world!" is it possible to "split" the variable so each character is its own variable so the output could look like:

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%.
我希望这会有所帮助!

To get the nth character in a string, use set char=%str:~n,1%.
I hope this was helpful!

这篇关于Windows批处理:将字符串拆分为单个字符并转换为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆