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-21 00:32
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Can set replace the "*" in %str% with empty? View 2,929 Replies 17
Original Poster Posted 2007-05-04 07:45 ·  中国 安徽 芜湖 电信
高级用户
★★
Credits 537
Posts 219
Joined 2006-10-31 21:08
19-year member
UID 69036
Gender Male
From 芜湖
Status Offline

@echo off
set "str=123*"
set str=%str:^*=%
echo %str%

I thought this would work, but unexpectedly it doesn't. I don't know why?
江湖远
碧空长
路茫茫

一个人漫无目的的奔跑,风,刺骨的冷....
Floor 2 Posted 2007-05-04 08:10 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
If you want something simpler:
@echo off
set "str=123*"
set str=%str:3*=3%
echo %str%
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
huzixuan +1 2007-05-05 08:41
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2007-05-04 08:17 ·  中国 安徽 芜湖 电信
高级用户
★★
Credits 537
Posts 219
Joined 2006-10-31 21:08
19-year member
UID 69036
Gender Male
From 芜湖
Status Offline
Uh, brother ccwan, one more question: in the following case, the asterisk appears randomly in the string.
If I want to replace only it, is there any other way?


@echo off
set "str=1234*567*89"
set str=%str:3*=3%
echo %str%
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
my3439955 +2 2007-06-05 19:39
江湖远
碧空长
路茫茫

一个人漫无目的的奔跑,风,刺骨的冷....
Floor 4 Posted 2007-05-04 09:13 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
http://www.cn-dos.net/forum/viewthread.php?tid=30232&fpage=2

Take a look at the part about extracting numbers in there.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
huzixuan +1 2007-05-05 08:42
Floor 5 Posted 2007-05-04 11:04 ·  中国 安徽 芜湖 电信
高级用户
★★
Credits 537
Posts 219
Joined 2006-10-31 21:08
19-year member
UID 69036
Gender Male
From 芜湖
Status Offline
Brother slore
I looked, but didn't find a solution.
江湖远
碧空长
路茫茫

一个人漫无目的的奔跑,风,刺骨的冷....
Floor 6 Posted 2007-05-04 11:42 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
That one can extract from arbitrary characters... removing just the asterisk should be possible, right?
Floor 7 Posted 2007-05-04 15:33 ·  中国 北京 顺义区 联通
中级用户
★★
替身
Credits 269
Posts 130
Joined 2007-03-23 21:41
19-year member
UID 82683
Gender Female
Status Offline
You can also try using FOR to do it, though it's rather rigid.

@echo off&setlocal
set "str=1234*567*89"
for /f "delims=* tokens=1,2*" %%a in ("%str%") do echo %%a%%b%%c
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
huzixuan +1 2007-05-05 08:42
Floor 8 Posted 2007-05-04 16:25 ·  中国 北京 顺义区 联通
中级用户
★★
替身
Credits 269
Posts 130
Joined 2007-03-23 21:41
19-year member
UID 82683
Gender Female
Status Offline
You can also try the method below; it's a bit more general than the one above.

@echo off&setlocal enabledelayedexpansion
set "str=1234*567*89.d;534*896"
set a=0&set c=0
:loop
if not defined c (echo %b%&goto:eof)
if not "!str:~%a%,1!" equ "*" (set b=!b!!str:~%a%,1!&set c=!str:~%a%!&set/a a+=1&goto:loop) else (set/a a+=1&goto:loop)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
huzixuan +2 2007-05-05 08:42
Floor 9 Posted 2007-05-04 16:52 ·  中国 湖北 武汉 电信
中级用户
★★
scriptlover
Credits 328
Posts 131
Joined 2007-03-25 22:17
19-year member
UID 82910
Gender Male
Status Offline
Question for brother ansipeter: what is the use of d[] in this line, set "str=1234*567*89.d[];534*896"??
scriptlover
Be with script forever!!
Floor 10 Posted 2007-05-04 17:11 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Why not use sed?
echo 1234*567*89.d;534*896|sed "s/\*//g"
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
huzixuan +1 2007-05-05 08:41
Floor 11 Posted 2007-05-05 08:41 ·  中国 安徽 芜湖 电信
高级用户
★★
Credits 537
Posts 219
Joined 2006-10-31 21:08
19-year member
UID 69036
Gender Male
From 芜湖
Status Offline
Looks like there really isn't a good way to replace it, so I'll have to consider using tools like sed to do it.
Thanks everyone for helping little brother think of solutions!!
江湖远
碧空长
路茫茫

一个人漫无目的的奔跑,风,刺骨的冷....
Floor 12 Posted 2007-05-05 13:42 ·  中国 广东 电信
初级用户
游荡的孤魂
Credits 79
Posts 29
Joined 2007-04-02 11:08
19-year member
UID 83714
Gender Male
From 湖南
Status Offline
Is there really no good way?

@echo off
set "str=12345*67*89"

:loop
for /f "delims=*" %%i in ("%str%") do call set "var=%%var%%%%i"
set "str_tmp=%str%"
set "str=%str:**=%"
if not "%str_tmp%"=="%str%" goto loop
echo "%var%"

pause
Floor 13 Posted 2007-05-05 17:20 ·  中国 安徽 芜湖 电信
高级用户
★★
Credits 537
Posts 219
Joined 2006-10-31 21:08
19-year member
UID 69036
Gender Male
From 芜湖
Status Offline
Impressive!
What's the principle behind it?
江湖远
碧空长
路茫茫

一个人漫无目的的奔跑,风,刺骨的冷....
Floor 14 Posted 2007-05-05 22:41 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
@echo off
set "str=1234*5***sdf*67*8*9*abcd*"

:loop
for /f "delims=* tokens=1*" %%a in ("%str%") do (
if not "%%a"=="%str%" set str=%%a%%b&& goto loop
)
echo %str%
pause




Thanks to digger for the correction

[ Last edited by slore on 2007-5-5 at 11:45 PM ]
Floor 15 Posted 2007-05-05 23:08 ·  中国 广东 电信
初级用户
游荡的孤魂
Credits 79
Posts 29
Joined 2007-04-02 11:08
19-year member
UID 83714
Gender Male
From 湖南
Status Offline
slore's code is even more concise than mine, respect.

However, && has to be placed right after %%b, otherwise a lot of extra spaces will appear. You can see it if you use echo "%str%".
Forum Jump: