简体   繁体   中英

Ruby string escape characters

I want to get the quoted string in the line below into a string exactly as-is, but I am tripping on the necessary escape sequences for Ruby. mycommand.cmd is really a wrapper for powershell.exe so I want to preserve everything between the quotes, and hold onto the escape characters that are already there.

mycommand.cmd "^|foreach-object { \"{0}=={1}\" -f $_.Name, $_.Found }"

Use single quotes :

ruby-1.9.3-p0 :001 > '^|foreach-object { \"{0}=={1}\" -f $.Name, $.Found }'
 => "^|foreach-object { \\\"{0}=={1}\\\" -f $.Name, $.Found }" 

The only escape characters in single quotes are : \\' and \\\\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM