Batch string interception

Batch string interception

insert image description here

In batch processing, the function of set is a bit complicated: setting variables, displaying the names and values ​​of environment variables, doing arithmetic operations, waiting for user input, string interception, and string replacement are one of our commonly used commands.

In terms of string interception, novices can easily extract wrong strings because they fail to notice the offset. Therefore, this post is specially opened to explain in detail the usage of set to intercept characters.

Let's look at an example first:

set str=123456789

Now, I need to extract the first character in the variable str, so how should I write the command?

set var=%str:~1,1%? I think this is likely to be the first reaction of many novices who have a rough understanding of the usage of set. In fact, this statement extracts the character "2", not the "1" we want. That is to say, set var=%str:~1,1% extracts the second character of the string instead of the first character. What is the reason?

It turns out that when the set command intercepts characters from left to right, it uses the first character of the entire string as the starting point to calculate the offset of the character to be intercepted. That is to say, when intercepting characters, set will calculate and extract The length of how many characters the first character of the subsequent string is offset from the first character of the entire string. It's important to note that set extracts characters by offset, not by their absolute position. Just keep this in mind, and you will no longer make mistakes on this issue when intercepting characters.

Now, we can express the command to intercept characters with a statement pattern, that is: set var=%str:~偏移量,长度%.

Let's interpret the meaning of this statement pattern in detail:

First, we need to assign the string to be operated on to a variable. In this statement, the string is assigned to the variable str; then, we need to determine which part of the string we want to extract, for example, to extract characters The second character of the string and the next 3 characters, or extract the fifth character of the string and the next 4 characters... Finally, calculate the offset and length, for example, to extract the second character of the string and the following 3 characters, then, that is to extract the character string whose offset relative to the first character is 1 and the length of the extracted character string is 4, the written statement is: set var=%str:~1,4%.

So far, we have only talked about very simple interception operations. If you encounter more complex extraction requirements, such as: extracting the second character and all characters after it, extracting the last 3 characters, extracting the second last character and The 3 characters before it, extract the string except the last 4 characters... so what should I do? Don't worry, the set command has fully considered our complex needs when it was designed. As long as we make a slight change to the character interception statement we mentioned just now, the task can be easily completed.

We know that the positive and negative of numbers can be represented by ± symbols, and similarly, the positive and negative of directions can also be marked by ±. When set is doing character interception, the ± symbol is introduced to indicate the direction of character interception: + is intercepted from left to right, and - is intercepted from right to left, so set var=%str:~1,4% is also possible It is written as set var=%str:~+1,+4%, but when intercepting from right to left, the situation has changed a little, that is: the starting point of the offset is the last character of the entire string to calculate. We can now answer some of the questions posed in the previous paragraph:

Extract the last 3 characters: set var=%str:~-3%
  extract the penultimate and the 3 characters before it: set var=%str:~-5,4%
  extract the string except the last 4 characters:set var=%str:~0,-4%

After reading the codes of the above three requirements, you may have new questions: Why is the first one only one number? The third last number is a minus sign, what does it mean?

It turns out that in set var=%str:~偏移量,长度%such a statement, if there is no comma and the length after it, it means to intercept all characters at and after the offset position, and if the value of the length is negative, it means to discard the last few characters.

Now, we can extract characters at any position (assuming set str=123456789):

① 提取1: set var=%str:~0,1% 或 set var=%str:~0,-8% 或 set var=%str:~-9,1%
② 提取2: set var=%str:~1,1% 或 set var=%str:~1,-7% 或 set var=%str:~-8,1%
③ 提取9: set var=%str:~8,1% 或 set var=%str:~8% 或 set var=%str:~-1,1% 或 set var=%str:~-1%
④ 提取123:set var=%str:~0,3% 或 set var=%str:~0,-6% 或 set var=%str:~-9,3%
⑤ 提取234:set var=%str:~1,3% 或 set var=%str:~1,-5% 或 set var=%str:~-8,3%
⑥ 提取789:set var=%str:~6,3% 或 set var=%str:~6% 或 set var=%str:~-3,3% 或 set var=%str:~-3%

Finally, let's summarize the rules of character interception:

1. String interception can be realized with set var=%str:~数值1,数值2%such a statement;
  2. The interception of characters is calculated by the offset, not by the absolute position of the character;
  3. When the value 1 is a positive number, it means from the left Intercept to the right; when the value 1 is negative, it means intercept from right to left;
  4. When the value 2 is positive, it means the length of the string to be intercepted; when it is negative, it means the last few characters to be discarded Length;
  5. When the value 2 and the comma before it do not exist, it means that the (value 1+1)th character and all characters after it are intercepted;

Case 1: Intercept from left to right, discard the first 4 characters

set var=123456789
echo %var:~4%
pause
-----------------------------
运行结果:
56789

Case 2: Intercept from left to right, only the first 3 characters

set var=123456789
echo %var:~0,3%
pause
-----------------------------
运行结果:
123

Case 3: Intercept from right to left, intercept 3 characters

set var=123456789
echo %var:~0,-3%
pause
-----------------------------
运行结果:
123456

Case 4: Discard the first digit from left to right, discard 3 digits from right to left

set var=123456789
echo %var:~1,-3%
pause
-----------------------------
运行结果:
123456
  • ~ The number after the tilde: a positive number indicates that the first digit of the variable is discarded;
  • , the number after the comma is a positive number, indicating that the first few digits after the discarding of the variable are taken;
  • , a negative number after the comma means to discard the last few digits of the variable

Guess you like

Origin blog.csdn.net/annita2019/article/details/129385662