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 realize string de-blanking in batch processing

2025-04-02 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 achieve string blanking in batch processing. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The code is as follows:

@ echo off

:: remove the left space

::

::

Set "str= ab c & >!"

For / f "tokens=*" I in ("str%") do echo "☆ I ☆"

Pause

Goto: eof

@ echo off

:: remove the left space

::

::

Set "str= ab c & >!"

: intercept

If "% str:~0,1%" = = "" set "str=%str:~1%" & goto intercept

Echo "☆ str% ☆"

Pause

Goto: eof

@ echo off

:: remove the right space

::

::

Set "str= ab c & >!"

: intercept

If "% str:~-1%" = = "" set "str=%str:~0,-1%" & goto intercept

Echo "☆ str% ☆"

Pause

Goto: eof

@ echo off

Remove the leading and trailing spaces

::

::

Set "str= ab c & >!"

: intercept_left

If "% str:~0,1%" = = "" set "str=%str:~1%" & goto intercept_left

: intercept_right

If "% str:~-1%" = = "" set "str=%str:~0,-1%" & goto intercept_right

Echo "☆ str% ☆"

Pause

Goto: eof

@ echo off

:: remove all spaces

::

::

Set "str= ab c & >!"

Set "str=%str: =%"

Echo "☆ str% ☆"

Pause

Goto: eof

@ echo% dbg% off

Cls & & echo. & & echo.

Redtek 2006 to remove the left and right variables with spaces and demonstrate the implementation of alternative methods

:: for the convenience of demonstration and other use, so the code in the "tag code segment" can be used independently

::

Set "var= My name is Redtek"

Rem Note: the parameters in the following Call are unquoted and are separated by comma spaces and so on.

Call: to the left space% var%

Echo. & & echo.

Call: go to the right space "% var%"

Rem removes the left and right spaces in the string

Rem for the convenience of demonstration and other use, the following steps will rewrite the code snippet to the left and right spaces, which can be used independently.

Echo. & & echo.

Call: go to the space "% var%"% var%

Goto: eof

Go to the left space

Rem uses the substitution principle to replace everything on the left side of the string that begins with ^% 1 variable content in the variable var with ^ 1 itself.

After rem is Call, all the spaces in the parameters are "lost" (using the parameter delimited feature).

Rem so, of course, the space to the left of ^% 1 to be located is gone.

Call set "variable with left space removed =% var:*%1=%1%%

Echo original string: [% var%]

Echo string after removing the left space: [% variable% after the left space]

Goto: eof

: go to the right space

Rem assigns the result to a variable that can be called at will.

Rem ~ nx takes advantage of the feature of "extending variables to a file name"

Since rem is a legal file name, of course, the space after the file name is invalid and useless ~:)

Set variable after removing the right space =% ~ nx1

Echo original string: [% var%]

Echo string after removing the right space: [% variable% after removing the right space]

Goto: eof

Go to the spaces on both sides

Rem principle: first go to the left space, and then to the right space, which is equivalent to the usefulness of the above demonstration code:)

Rem in the absence of the above two paragraphs to the left and right spaces of the code, the following independent demonstration:

Set the variable after removing the left and right spaces =% ~ nx1

Call set "variable after removing left and right spaces =%% variable after removing left and right spaces: *% 2% 2%%"

Echo original string: [% var%]

Echo string after removing left and right spaces: [% variable% after removing left and right spaces]

Goto: eof

This is the end of this article on "how to realize string blanking in batch processing". I hope the above content can be helpful 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