China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-28 01:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to implement variable substitution for variables? View 3,207 Replies 4
Original Poster Posted 2006-10-11 06:58 ·  中国 辽宁 沈阳 联通
新手上路
Credits 17
Posts 6
Joined 2006-09-21 23:27
19-year member
UID 63293
Status Offline
set a=123
set b=1234
How to replace the same parts in b with 0 using variable a?
Is it like this? How to write variable A?
echo %b:a=0%
Floor 2 Posted 2006-10-11 08:46 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
The code is as follows:


@echo off
setlocal enabledelayedexpansion
set a=123
set b=1234
set "b=!b:%a%=0!"
echo %b%
pause
Floor 3 Posted 2006-10-11 09:33 ·  中国 浙江 衢州 电信
银牌会员
★★★
Credits 1,270
Posts 548
Joined 2004-05-31 00:00
22-year member
UID 25754
Gender Male
Status Offline
If
set a=123
set b=1234
And finally, how to write the code so that echo %b% displays 0004?
Floor 4 Posted 2006-10-11 09:38 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by weilong888 at 2006-10-11 09:33:
If
set a=123
set b=1234
And finally, how to make echo %b% display 0004?
How should this code be written?

Change set "b=!b:%a%=0!" to set "b=!b:%a%=000!" to achieve it.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2006-10-12 07:01 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
It is also possible to use environment variables without enabling delay, but there are relatively large limitations:

set "a=123"
set "b=1234"
echo %b:%a%=000% | findstr .
Forum Jump: