Dos Intercept string method

Source: Internet
Author: User
Tags abs
For a detailed description of the string problem in the DOS (Windows command line) intercept variable, you need a friend to refer to the next

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 the first character is:%str:~0,1%

The
Echo first two characters are:%str:~0,2%


Echo Head 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:%str:~0,-3%


Echo the 4th character 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 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 the first character is:%str:~0,1%
The REM pointer is 0, and the offset address is 1, Starting at No. 0, the 1-bit
Echo-first two characters are:%str:~0,2%
The REM pointer is 0, the offset address is 2, starting at No. 0, and 2-bit
Echo-head 5 characters:%str:~0,5%
rem   The pointer is 0, the offset address is 5, starting at No. 0, and taking the 5-bit
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, then subtract the back "abs" ( k) Bit ". So the sentence we can do is as follows: start at the No. 0 bit with all of its characters
: 123456789 then subtract the ABS (k) bit from the back, so the final result is: 12345678
Echo Removes the last 3 characters from the string:%str:~0,-3%
rem   This sentence to explain IBID. ↑
Echo Last character:%str:~-1%
rem   parameter "N," and "K" can be the default, the default "N," can be understood as follows: from ABS (k) The bit begins to take all of its
Echo's penultimate 4th and the following characters are:%str:~-4%
rem         Interpretation ditto ↑
Echo The last character is:%str:~-1,1%
rem   n is negative, it means to intercept the character from the beginning, take the K bit (at which point N should start at 1)
Echo last character:%str:~-1,2%
rem    Interpretation ditto ↑
Echo 4th character:%str:~-4,1%
rem   Interpretation ditto ↑
Echo Countdown 4th and the following 1 characters are:%str:~-4,2%
REM    Interpretation ditto ↑
Echo 4th and the following 2 characters are:%str:~-4,3%
Rem Interpretation ibid. ↑
pause

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.