Batch string space removal implementation code [different parts]

Source: Internet
Author: User

CopyCode 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 spaces at the beginning and end
::
::
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.
: Demonstration of variable tests and alternative methods for removing left and right spaces in redtek 2006
: It is convenient for demonstration and other functions, 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 not enclosed in quotation marks. The parameters are separated by commas (,) and spaces.
Call: remove the left space % var %
Echo. & Echo.
Call: remove the right space "% var %"
Rem removes the Left and Right spaces in the string
For the convenience of demonstration and other functions, the following steps re-compile the code segment with left and right spaces, which can be used independently.
Echo. & Echo.
Call: remove the space "% var %" % var %
Goto: EOF

: Left-side Space

Rem uses the replacement principle to replace everything on the left of the string starting with ^ % 1 in the variable VAR with ^ 1 itself.
Rem, because after being called, all spaces in the parameters are "lost" (using the feature of parameter separation ),
Rem, the space on the left of ^ % 1 to be located is gone.

Call set "variables after left space removal =%% var: * % 1 = % 1% %

Echo original string: [% var %]
Echo string after left space removal: [% variable after left space removal %]
Goto: EOF

: Right-side Space

Rem assigns the result to the variable, which can be called at will.
Rem ~ NX uses the feature of "extending a variable to a file name,
Since REM is a valid file name, the space behind the file name is invalid and useless ~ :)

Set variable after right space removal = % ~ Nx1

Echo original string: [% var %]
Echo: [% variable % after right space removal]
Goto: EOF

: Remove spaces on both sides

Rem principle: first go to the left space and then to the right space, which is equivalent to the use of the above DEMO code :)
The following is an independent demonstration of REM without the code for removing spaces from the left and right in the preceding two sections:

Set variables after spaces are removed from left and right = % ~ Nx1
Call set "variables with spaces between left and right =%% variables with spaces between left and right: * % 2 = % 2% %"

Echo original string: [% var %]
Echo string after left and right spaces: [% variable after left and right spaces]
Goto: EOF

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.