Ruby string escape characters

帅比萌擦擦* 提交于 2019-12-24 10:48:16

问题


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 }"

回答1:


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 \\



来源:https://stackoverflow.com/questions/11270944/ruby-string-escape-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!