中国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-03 10:50
48,038 topics / 350,123 posts / today 0 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » Detailed Explanation about the problem of intercepting strings in variables
Printable Version  4,224 / 6
Floor1 hankee Posted 2007-02-23 02:39
初级用户 Posts 10 Credits 36
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 ]
Floor2 newaifi Posted 2007-02-23 21:54
初级用户 Posts 67 Credits 154
The building owner's explanation is detailed and clear. My admiration for the building owner is like a surging river, continuous, and like...
Floor3 NaturalJ0 Posted 2007-02-23 23:43
银牌会员 Posts 533 Credits 1,181
It's written very well and is very convenient for learning.
Floor4 yingzai621 Posted 2007-06-17 21:41
新手上路 Posts 1 Credits 2
Leave a message for future reference!
Floor5 hackerflysky Posted 2007-06-18 01:12
初级用户 Posts 20 Credits 50 From 湖南常宁
Bookmarked
Floor6 blogsss Posted 2007-06-30 12:15
初级用户 Posts 14 Credits 36
Nice
Floor7 rivch Posted 2010-05-17 00:44
新手上路 Posts 8 Credits 11
Roaring river water, no explanation、
Just studying this part...
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023