To illustrate the problem of intercepting a string in a variable, the following example has been cited:
Copy Code code as follows:
@echo off
Set str=123456789
Echo's first character is:%str:~0,1%
The echo first two characters are:%str:~0,2%
The Echo header 5 characters are:%str:~0,5%
Echo removes the string after the last character:%str:~0,-1%
Echo removes the last 3 characters after the string is:%str:~0,-3%
The 4th character of Echo is:%str:~3,1%
Echo 4th and the following 3 characters are:%str:~3,4%
The last character of Echo is:%str:~-1%
The last character of Echo is:%str:~-1,1%
The last character of Echo is:%str:~-1,2%
The 4th character of Echo's countdown is:%str:~-4,1%
The 4th and subsequent characters of ECHO are:%str:~-4%
The 4th and subsequent 1 characters of Echo are:%str:~-4,2%
The 4th and subsequent 2 characters of Echo are:%str:~-4,3%
Pause
To illustrate this issue, I am here to take the batch processing character, to do a further explanation, I hope to enlighten the novice
As follows:
Echo%var:~n,k%
Here's a description of each parameter: "%var", which is the string from which we want to intercept characters. "~" to take the word
Character marker (as I understand it), "n" We interpret it as a pointer, "K" we interpret as an offset address. (Note
: Pointers and offset addresses are zero-based.
We still use the example of NAMEJM moderator to do the following explanation:
Copy Code code as follows:
@echo off
Set str=123456789
REM defines a str string of 123456789
Echo's first character is:%str:~0,1%
The REM pointer is 0, and the offset address is 1, starting at the No. 0 bit, and taking 1 bits
The echo first two characters are:%str:~0,2%
The REM pointer is 0, and the offset address is 2, starting at the No. 0 bit, and taking 2 bits
The Echo header 5 characters are:%str:~0,5%
The REM pointer is 0, and the offset address is 5, starting at the No. 0 bit, and taking 5 bits
Echo removes the string after the last character:%str:~0,-1%
rem when "K" is a negative value, we can understand this: start at the beginning of the pointer to all the characters behind it, and then subtract the back "abs (k) bit". So we can do the following to explain: start with the No. 0 bit of all the characters
is: 123456789 then subtracts the ABS (k) bit from the back, so the final result is: 12345678
Echo removes the last 3 characters after the string is:%str:~0,-3%
REM the sentence explains ibid. ↑
The last character of Echo is:%str:~-1%
rem Parameters "N," and "K" can all be the default, the default "N," can be understood as: starting from the ABS (k) bit all
The 4th and subsequent characters of ECHO are:%str:~-4%
REM Explanation ditto ↑
The last character of Echo is:%str:~-1,1%
When REM n is negative, it means that the character is intercepted from the beginning, and the K bit is taken (at which point N should start at 1)
The last character of Echo is:%str:~-1,2%
REM Explanation ditto ↑
The 4th character of Echo's countdown is:%str:~-4,1%
REM Explanation ditto ↑
The 4th and subsequent 1 characters of Echo are:%str:~-4,2%
REM Explanation ditto ↑
The 4th and subsequent 2 characters of Echo are:%str:~-4,3%
REM Explanation ditto ↑
Pause