Question from professor: Special Characters are always a problem for compilers.
ID: 3664146 • Letter: Q
Question
Question from professor: Special Characters are always a problem for compilers. For example, using a quotation mark inside of a String is problematic, since you need quotation marks to define the string. The usual solution is to double the quotation mark to indicate it is not the end of the string (for example, "this line is 3"" long"). A better solution is to use a special intrinsic list provided by Microsoft. What is the name of the list? Give an example of its use.
Would this be referring to the ControlChar class? Is it called a Control Characters List? Thank you!
Explanation / Answer
To generally answer the question of how to escape double-quotes inside a double-quoted string using cmd.exe, the Windows command-line interpreter (whether on the command line - often still mistakenly called the "DOS prompt" - or in a batch file)see bottom for a look at PowerShell:
tl;dr:
Use "" when passing a string to a(nother) batch file or applications created withMicrosoft's C/C++/.NET compilers, :
Example: foo.bat "We had 3"" of rain."
The following applies to batch files only:
"" is the only way to get the command interpreter (cmd.exe) to treat the whole double-quoted string as a single argument.
Sadly, however, not only are the enclosing double-quotes retained (as usual), but so are the doubled escaped ones, so obtaining the intended string is a two-step process; e.g., assuming that the double-quoted string is passed as the 1st argument, %1:
set "str=%~1" removes the enclosing double-quotes; set "str=%str:""="%" then converts the doubled double-quotes to single ones.
Be sure to use the enclosing double-quotes around the assignment parts to prevent unwanted interpretation of the values.
" is required by many other programs, (e.g., Perl, Python, Ruby, and even Microsoft's own PowerShell(!)), but ITS USE IS NOT SAFE:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.