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-08-12 14:00
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion] Question about character extraction View 777 Replies 2
Original Poster Posted 2010-04-28 15:22 ·  中国 广东 东莞 电信
初级用户
Credits 23
Posts 13
Joined 2009-06-02 07:33
17-year member
UID 146527
Gender Male
Status Offline
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?
我不是人
Floor 2 Posted 2010-04-28 17:44 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
set123456789
12345
56789
34567
567
5
56

The result is like this? That's perfectly normal. I can't see what the problem is.
Floor 3 Posted 2010-04-29 17:35 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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.
Forum Jump: