中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-12 13:15
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » [Discussion] Question about character extraction
Printable Version  776 / 2
Floor1 lzbow Posted 2010-04-28 15:22
初级用户 Posts 13 Credits 23
Please look at this
@echo off
echo set123456789>123.txt
set b=123456789
echo %b:~0,5% >>123.txt
echo %b:~-5% >>123.txt
echo %b:~2,5% >>123.txt
echo %b:~-5,3% >>123.txt
echo %b:~-5,-4% >>123.txt
echo %b:~-5,-3% >>123.txt
Why is it that
echo %b:~0,5% >>123.txt
echo %b:~-5% >>123.txt
echo %b:~2,5% >>123.txt
echo %b:~-5,3% >>123.txt
directly display what they extract,
but
echo %b:~-5,-4% >>123.txt
echo %b:~-5,-3% >>123.txt
display what remains after their extraction?
Floor2 Hanyeguxing Posted 2010-04-28 17:44
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
set123456789
12345
56789
34567
567
5
56

The result is like this? That's perfectly normal. I can't see what the problem is.
Floor3 Hanyeguxing Posted 2010-04-29 17:35
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
About "string extraction": %a:~]%
  The square brackets indicate optional parts, % is the variable identifier, a is the variable name and cannot be omitted, the colon is used to separate the variable name and the description part, and the symbol ~ can simply be understood as "offset". m is the offset (default is 0, meaning extraction starts from the 1st character), and n is the extraction length (default is all).
%a:~0,n% takes the leftmost n characters
%a:~-m% takes the rightmost m characters
%a:~m,n% takes n characters starting from position m+1
%a:~m,-n% starts from position m+1, up to the (n+1)th character from the end
%a:~m % takes everything to the right starting from position m+1.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023