DOS truncated string ~

Source: Internet
Author: User
Tags truncated
@ Echo off

Set STR = 123456789

The first character of ECHO is: % STR :~ 0, 1%
The echo header contains two characters: % STR :~ 0, 2%
The first five characters of ECHO are: % STR :~ 0, 5%
The echo string after removing the last character is: % STR :~ 0,-1%.
The string after removing the last 3 Characters in ECHO is: % STR :~ 0,-3%.
Echo 4th characters: % STR :~ 3, 1%
The echo 4th and later 3 characters are: % STR :~ 3,4%
The last echo character is: % STR :~ -1%
The last echo character is: % STR :~ -1,1%
The last echo character is: % STR :~ -1,2%
Echo countdown 4th characters: % STR :~ -4,1%
The last 4th and subsequent characters of ECHO are: % STR :~ -4%
The last 4th echo characters and the following 1 character are: % STR :~ -4,2%
The last 4th echo characters and the following two characters are: % STR :~ -4,3%

Pause

In order to illustrate this problem, I will take the batch processing characters here for further explanation, hoping to inspire new users.

As follows:


Echo % var :~ N, K %


Here we will explain each parameter: "% Var", that is, the string from which we want to intercept characters ."~ "Take words
Mark (I understand this), "N" is understood as a pointer, and "K" is understood as an offset address.
: The pointer and offset address are both from scratch)

We still use the namejm moderator's example to describe:
@ Echo off
Set STR = 123456789

Rem defines a STR string as 123456789
The first character of ECHO is: % STR :~ 0, 1%
The REM pointer is 0 and the offset address is 1, that is, starting from 0th bits and taking 1 bits

The echo header contains two characters: % STR :~ 0, 2%

The REM pointer is 0 and the offset address is 2, that is, starting from 0th bits and taking 2 bits

The first five characters of ECHO are: % STR :~ 0, 5%

Rem
The pointer is 0, and the offset address is 5, that is, starting from 0th bits and taking 5 bits
The echo string after removing the last character is: % STR :~ 0,-1%.
When "K" is a negative value, we can understand as follows: from the beginning of the pointer, take all the characters after it, and then subtract
Next, "ABS (k) bit"... so this sentence can be interpreted as follows: Get all its characters from the first byte
Is: 123456789 and then subtract the ABS (k) bit from the back, so the final result is: 12345678
The string after removing the last 3 Characters in ECHO is: % STR :~ 0,-3%.

Rem
This sentence is the same as above.
The last echo character is: % STR :~ -1%
Rem
Both the "N," and "K" parameters are default values. The default value is "N," which can be understood as: Get all the values starting from the ABS (k) bit.
The last 4th and subsequent characters of ECHO are: % STR :~ -4%

Rem
Same as above
The last echo character is: % STR :~ -1,1%
Rem
When N is a negative value, it indicates that the characters are truncated from the back and k characters are taken (at this time, N should start from 1)
The last echo character is: % STR :~ -1,2%
Rem
Same as above
Echo countdown 4th characters: % STR :~ -4,1%

Rem
Same as above
The last 4th echo characters and the following 1 character are: % STR :~ -4,2%
Rem
Same as above
The last 4th echo characters and the following two characters are: % STR :~ -4,3%
Rem interpretation is the same as above.
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.