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-03 09:53
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Detailed Explanation about the problem of intercepting strings in variables View 4,219 Replies 6
Original Poster Posted 2007-02-23 02:39 ·  中国 江苏 徐州 电信
初级用户
Credits 36
Posts 10
Joined 2007-02-10 06:32
19-year member
UID 79139
Gender Male
Status Offline
Moderator namejm once used the following example to explain the problem of intercepting strings in variables:

@echo off
set str=123456789
echo The first character is: %str:~0,1%
echo The first two characters are: %str:~0,2%
echo The first five characters are: %str:~0,5%
echo The string after removing the last character is: %str:~0,-1%
echo The string after removing the last 3 characters is: %str:~0,-3%
echo The 4th character is: %str:~3,1%
echo The 4th character and the following 3 characters are: %str:~3,4%
echo The last character is: %str:~-1%
echo The last character is: %str:~-1,1%
echo The last character is: %str:~-1,2%
echo The 4th character from the end is: %str:~-4,1%
echo The characters from the 4th from the end onwards are: %str:~-4%
echo The 1 character from the 4th from the end onwards is: %str:~-4,2%
echo The 2 characters from the 4th from the end onwards are: %str:~-4,3%
pause
To explain this problem, I will further explain batch processing character extraction here, hoping to inspire newcomers.

The following is:
echo %var:~n,k%
Here, we explain each parameter: "%var" is the string from which we want to extract characters. "~" is the character extraction identifier (this is how I understand it). "n" we understand it as the pointer, and "k" we understand it as the offset address. (Note: Both the pointer and the offset address are counted from zero.)
We still use Moderator namejm's example for illustration:

@echo off
set str=123456789

rem Define a str string as 123456789

echo The first character is: %str:~0,1%

rem The pointer is 0, the offset address is 1, that is, starting from position 0, take 1 character

echo The first two characters are: %str:~0,2%

rem The pointer is 0, the offset address is 2, that is, starting from position 0, take 2 characters

echo The first five characters are: %str:~0,5%

rem The pointer is 0, the offset address is 5, that is, starting from position 0, take 5 characters

echo The string after removing the last character is: %str:~0,-1%

rem When "k" is negative, we can understand it like this: start taking all characters from the pointer, then subtract the last abs(k) bits.. So this sentence can be explained as follows: start taking all characters from position 0 as: 123456789, then subtract abs(k) bits from the end, so the final result is: 12345678

echo The string after removing the last 3 characters is: %str:~0,-3%

rem The explanation is the same as above ↑

echo The last character is: %str:~-1%

rem Both "n," and "k" can be omitted. When "n," is omitted, it can be understood as: start taking all characters from the abs(k) bit

echo The characters from the 4th from the end onwards are: %str:~-4%

rem The explanation is the same as above ↑

echo The last character is: %str:~-1,1%

rem When n is negative, it means intercepting characters from the end, take k bits (at this time, n should be counted from 1)

echo The last character is: %str:~-1,2%

rem The explanation is the same as above ↑

echo The 4th character from the end is: %str:~-4,1%

rem The explanation is the same as above ↑

echo The 1 character from the 4th from the end onwards is: %str:~-4,2%

rem The explanation is the same as above ↑

echo The 2 characters from the 4th from the end onwards are: %str:~-4,3%

rem The explanation is the same as above ↑
pause



I hope the above explanation is helpful to everyone. If there are any omissions, please criticize and correct, thank you~

[ Last edited by hankee on 2007-2-22 at 01:42 PM ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
NaturalJ0 +8 2007-02-23 23:42
zqdarkday +2 2007-06-30 20:00
Floor 2 Posted 2007-02-23 21:54 ·  中国 浙江 温州 电信
初级用户
★★
Credits 154
Posts 67
Joined 2006-10-31 00:50
19-year member
UID 68933
Gender Male
Status Offline
The building owner's explanation is detailed and clear. My admiration for the building owner is like a surging river, continuous, and like...
Floor 3 Posted 2007-02-23 23:43 ·  中国 江苏 苏州 联通
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
It's written very well and is very convenient for learning.
Floor 4 Posted 2007-06-17 21:41 ·  中国 北京 联通
新手上路
Credits 2
Posts 1
Joined 2007-05-26 16:57
19-year member
UID 89384
Gender Male
Status Offline
Leave a message for future reference!
Floor 5 Posted 2007-06-18 01:12 ·  中国 广东 东莞 电信
初级用户
Credits 50
Posts 20
Joined 2007-06-17 00:18
19-year member
UID 91565
Gender Male
From 湖南常宁
Status Offline
Bookmarked
Floor 6 Posted 2007-06-30 12:15 ·  中国 北京 联通
初级用户
Credits 36
Posts 14
Joined 2007-06-28 16:09
19-year member
UID 92592
Gender Male
Status Offline
Nice
Floor 7 Posted 2010-05-17 00:44 ·  中国 浙江 温州 瑞安市 电信
新手上路
Credits 11
Posts 8
Joined 2010-01-29 22:21
16-year member
UID 159746
Gender Male
Status Offline
Roaring river water, no explanation、
Just studying this part...
Forum Jump: