与符号为不同的部分分离变量 [英] separating variable with a symbol into different parts

查看:128
本文介绍了与符号为不同的部分分离变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,我希望能够给一个变量分成两个或三个部分,当存在一个符号划分它们。例如,如果我有一个看起来像这样的字符串:
VAR1,VAR2;

using batch, i want to be able to separate a variable into two or three parts, when there is a symbol dividing them. for example if i have the string which looks like this: var1;var2;

?我怎样才能VAR1成为变量和var2成为一个不同的。

how can i get var1 to become variable and var2 to become a different one.

由于提前

推荐答案

令牌= 1,2 并创建两个循环变量 %%我 %%Ĵ&安培;拆分字符串两部分,作为分隔符;

Tokens=1,2 does create the two for loop variables %%i and %%j& splits string in two parts, separated by the delimiter ;:

@echo off &setlocal
set "string=var1;var2;"
for /f "tokens=1,2 delims=;" %%i in ("%string%") do set "variable1=%%i" &set "variable2=%%j"
echo variable1: %variable1%
echo variable2: %variable2%
endlocal
pause

有关更动态方式使用:

@echo off &setlocal enabledelayedexpansion
set "string=var1;var2;"

set /a count=0
for %%i in (%string%) do (
    set /a count+=1
    set "variable!count!=%%i"
)
echo found %count% variables
for /l %%i in (1,1,%count%) do (
    echo variable%%i: !variable%%i!
)
endlocal

这篇关于与符号为不同的部分分离变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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