    『楼 主』:
关于截取变量中字符串问题的详细说明
使用 LLM 解释/回答一下
namejm版主为了说明截取变量中字符串的问题曾经举过如下这个例子:
@echo off
set str=123456789
echo 第一个字符为:%str:~0,1%
echo 头两个字符为:%str:~0,2%
echo 头5个字符为:%str:~0,5%
echo 去掉最后一个字符后的字符串为:%str:~0,-1%
echo 去掉最后3个字符后的字符串为:%str:~0,-3%
echo 第4个字符为:%str:~3,1%
echo 第4个及其之后的3个字符为:%str:~3,4%
echo 最后一个字符为:%str:~-1%
echo 最后一个字符为:%str:~-1,1%
echo 最后一个字符为:%str:~-1,2%
echo 倒数第4个字符为:%str:~-4,1%
echo 倒数第4个及其之后的字符为:%str:~-4%
echo 倒数第4个及其之后的1个字符为:%str:~-4,2%
echo 倒数第4个及其之后的2个字符为:%str:~-4,3%
pause
为了说明这个问题,我在这里把批处理取字符,做下进一步的解释,希望对新手有所启发
如下:
echo %var:~n,k%
我们在这里对每个参数做个说明:"%var",即我们要从中要截取字符的字符串." ~ "取字
符标志符(我是这么理解的),"n" 我们将其理解为指针,"k"我们将其理解为偏移地址.(注
:指针和偏移地址都是从零开始数的)
我们还是用namejm版主的例子做下说明:
@echo off
set str=123456789
rem 定义一个str字符串为123456789
echo 第一个字符为:%str:~0,1%
rem 指针为0,偏移地址为1,即从第0位开始,取1位
echo 头两个字符为:%str:~0,2%
rem 指针为0,偏移地址为2,即从第0位开始,取2位
echo 头5个字符为:%str:~0,5%
rem 指针为0,偏移地址为5,即从第0位开始,取5位
echo 去掉最后一个字符后的字符串为:%str:~0,-1%
rem 当"k"为负值时,我们可以这样理解:从指针开始处开始取其后面所有字符,然后减去
后面"abs(k)位"..所以这个句字我们可以做如下解释:从第0位开始取其全部字符
为:123456789然后从后面减去abs(k)位,所以最后结果为:12345678
echo 去掉最后3个字符后的字符串为:%str:~0,-3%
rem 该句解释同上↑
echo 最后一个字符为:%str:~-1%
rem 参数"n,"和"k"都可以为缺省,缺省"n,"时可以理解为:从abs(k)位开始取其全部
echo 倒数第4个及其之后的字符为:%str:~-4%
rem 解释同上↑
echo 最后一个字符为:%str:~-1,1%
rem n为负值时,表示从后面开始截取字符,取k位(此时n应从1开始数)
echo 最后一个字符为:%str:~-1,2%
rem 解释同上↑
echo 倒数第4个字符为:%str:~-4,1%
rem 解释同上↑
echo 倒数第4个及其之后的1个字符为:%str:~-4,2%
rem 解释同上↑
echo 倒数第4个及其之后的2个字符为:%str:~-4,3%
rem 解释同上↑
pause
希望上述说明,对大家有所帮助,如有疏漏之处,还请大家批评指正,谢谢~
Last edited by hankee on 2007-2-22 at 01:42 PM ]
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 ]
|