Batch BAT substitution and interception string usage t1=%a:~3% What does that mean _dos/bat

Source: Internet
Author: User
I. Substitution usage
Cases

Copy Code code as follows:

@echo off
Set A=belcome to CMD borld!
Set temp=%a:b=w%
Echo%temp%
Pause


will display welcome to CMD world! The b in variable A is replaced with W.

ii. Interception of usage
The first cursor at the top of the string represents the No. 0 digit, and the cursor after the first character represents the number one, and so on.
To better understand the interception usage, here is a schematic diagram of the number of superhero digits in the string:
S U P e r h e r O

0 1 2 3 ...-3-2-1 No indication

Cases

Copy Code code as follows:

@echo off
Set A=superhero
Set temp=%a:~0,5%
Echo%temp%
Pause


The super shows all the elements that are included in the No. 0 and 5th digits of variable A.

If the following

Copy Code code as follows:

@echo off
Set A=superhero
Set temp=%a:~3%
Echo%temp%
Pause
[HTML]

Displays Perhero, which shows all characters after the third digit of variable A.



If the following

[Code]
@echo off
Set A=superhero
Set temp=%a:~-3%
Echo%temp%
Pause


The Ero display shows the penultimate three characters of variable A.

If the following

Copy Code code as follows:

@echo off
Set A=superhero
Set temp=%a:~0,-3%
Echo%temp%
Pause

The display of SuperH shows the No. 0 bit of variable A and all the characters contained in the first-3 digits.

Bat Intercept String instance

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 two characters of Echo are:%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 take all the characters behind it, and then subtract
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.