How to remove quotes from user-passed parameters

Source: Internet
Author: User
Keywords Quotes nbsp; variables CAN strings

In many cases, we need to remove the quotation marks that might exist in a string, and then add our own citation
number to make the special characters (Command connector &, |, &&, | |, the command line argument space, tab,
; =, Character escapes ^, ", variable, and so on", and so on, lose a specific function, and as a
normal character becomes an integral part of the string.

One, there are three simple ways to remove the quotation marks in a string, and they are similar in function to each other, except that they use the
situation differently and can handle most situations.

1-1, if the string exists in command line parameter%1, you can use%~1 to remove the first external quotation marks, such as
the string does not change without the outside quotes,

1-2, if the string exists in the for substitution variable%%i, you can use the%%~  I remove the first external quotation mark,
if there are no outside quotes then the string does not change;

1-3, if the string exists in the environment variable%temp%, you can use%temp: "=% remove all of the
quotes, and if there are no quotes, the string does not change;

1-4. The above three schemes can be used to a certain extent because they are a type of variable, and
can be transferred to one another by code or snippets similar to the following:

&http://www.aliyun.com/zixun/ Aggregation/37954.html ">nbsp;     1-4-1, for substitution variable switch command-line arguments: Call:dequote%%i
      1-4-2, environment variable switch command line arguments: call:dequote %temp%
      1-4-3, command-line arguments for substitution variable: for%%i in (% 1) do ...
      1-4-4, environment variable for substitution variable: for%%i in (%temp%) do ...
      1-4-5, command-line arguments to environment variables: set temp=%1
      1-4-6, for substitution variable environment variable: for ... Set temp=%%i

Two, if the string quotation marks are complex, or if we have special requirements for the position of the caret,
or some control characters may appear in the string, you can dump the string first through the corresponding method in 1-4
to the environment variable. Processing is done using or combining the following scenarios:

2-1, you can use the set var=%var:~1% to remove the first quotation mark of the environment variable var string, or the first character to be removed if the string first
does not exist;

2-2, you can use the set% var:* "=% Remove the first quotation mark of the environment variable var string, if the string first does not
the value of the variable is not changed;

2-3, you can use set var=%var:~0,-1% to remove the last quotation mark at the end of the environment variable var string, such as
The end of the string does not exist the last one is removed;

2-4, you can use the set "var=%var%" to remove the last quotation mark at the end of the environment variable var string, if the string tail
does not exist, the environment variable is emptied,

2-5, you can use the set var= %var:~1,-1% remove the most outer pair of quotes from the environment variable Var string, such as
the outer side of the string is off;

2-6, you can use the%var:* "=set" var=% to remove the most outer pair of quotes from the environment variable var string, if
A syntax error occurs when there is no quotation mark on the outer side of the string;

2-7, you can use Set "Var=%var:" =% to remove all quotes that may appear in the environment variable var string
If the string does not appear in quotes the value of the variable is unchanged; unlike 1-3, It allows string matching quotes to appear in
Special control characters;

Related Article

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.