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 ]
@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 ]
