Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to replace and intercept strings in batch BAT

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to replace and intercept strings in batch BAT. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

I. alternative usage

Example

The code is as follows:

@ echo off

Set a=belcome to CMD borld!

Set temp=%a:b=w%

Echo temp%

Pause

Welcome to CMD world will be displayed! That is, replace the b in the variable a with w.

II. Interception and usage

The cursor at the front of the string represents bit 0, the cursor after the first character represents the first bit, and so on.

To better understand the use of interception, the following is a schematic diagram of the number of superhero bits in a string:

S u p e r h e r o

0 1 2 3. -3-2-1 has no representation

Example

The code is as follows:

@ echo off

Set a=superhero

Set temp=%a:~0,5%

Echo temp%

Pause

The super will be displayed, showing all the elements included in bits 0 to 5 of the variable a.

If the following

The code is as follows:

@ echo off

Set a=superhero

Set temp=%a:~3%

Echo temp%

Pause

[html]

Perhero will be displayed, that is, all characters after the third digit of the variable a will be displayed.

If the following

[code]

@ echo off

Set a=superhero

Set temp=%a:~-3%

Echo temp%

Pause

The ero is displayed, which shows the last three characters of the variable a.

If the following

The code is as follows:

@ echo off

Set a=superhero

Set temp=%a:~0,-3%

Echo temp%

Pause

The superh is displayed, which shows all the characters contained between bit 0 and bit-3 of the variable a.

Bat intercepts a string instance

The code is as follows:

@ echo off

Set str=123456789

The first character of echo is:% str:~0,1%

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

The first five characters of echo are:% str:~0,5%

The string after the last character is removed by echo is:% str:~0,-1%

The string after the last three characters are removed by echo is:% str:~0,-3%

The fourth character of echo is:% str:~3,1%

The 4th and the following 3 characters of echo 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 penultimate character of echo is:% str:~-4,1%

The penultimate character of echo and the characters after it are:% str:~-4%

The penultimate character of echo and the 1 character after it are:% str:~-4,2%

The penultimate character of echo and the following two characters are:% str:~-4,3%

Pause

In order to illustrate this problem, I take the batch processing characters here and give a further explanation, hoping to enlighten the novice.

It is as follows:

Echo var:~n,k%

Here we give a description of each parameter: "% var", that is, the string from which we want to intercept characters. "~" take the word

Sign sign (as I understand it), "n" we understand it as a pointer, "k" we understand it as an offset address. (note

Pointers and offset addresses are counted from zero)

Let's use the example of the namejm moderator to illustrate:

The code is as follows:

@ 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 with bit 0, take 1 bit

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

The rem pointer is 0 and the offset address is 2, that is, starting with bit 0, take 2 bits

The first five characters of echo are:% str:~0,5%

The rem pointer is 0 and the offset address is 5, that is, starting with bit 0, take 5 bits

The string after the last character is removed by echo is:% str:~0,-1%

Rem when "k" is negative, we can understand it like this: take all the characters after it from the beginning of the pointer, and then subtract

After "abs (k) bit".. So we can explain this sentence as follows: take all its characters from bit 0.

Is: 123456789 and then subtracts the abs (k) bit from behind, so the final result is: 12345678

The string after the last three characters are removed by echo is:% str:~0,-3%

Rem this sentence is explained as ↑ above.

The last character of echo is:% str:~-1%

The rem parameters "n," and "k" can both be the default, and the default "n," can be understood as taking all of them from the abs (k) bit.

The penultimate character of echo and the characters after it are:% str:~-4%

Rem interpretation is the same as ↑ above

The last character of echo is:% str:~-1,1%

When rem n is negative, it means that characters are intercepted from behind and k bits are taken (n should be counted from 1 in this case)

The last character of echo is:% str:~-1,2%

Rem interpretation is the same as ↑ above

The penultimate character of echo is:% str:~-4,1%

Rem interpretation is the same as ↑ above

The penultimate character of echo and the 1 character after it are:% str:~-4,2%

Rem interpretation is the same as ↑ above

The penultimate character of echo and the following two characters are:% str:~-4,3%

Rem interpretation is the same as ↑ above

Pause

This is the end of this article on "how to replace and intercept strings in batch BAT". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report