Ruby 字符串转义字符

标签 ruby string escaping

我想将下面一行中带引号的字符串完全按原样转换成字符串,但我在 Ruby 的必要转义序​​列上绊倒了。 mycommand.cmd 实际上是 powershell.exe 的包装器,所以我想保留引号之间的所有内容,并保留已经存在的转义字符。

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

最佳答案

使用单引号:

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

单引号中唯一的转义字符是:\'\\

关于Ruby 字符串转义字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11270944/

相关文章:

ruby-on-rails - 代码读取 : why is the following code implemented like this?

ruby - Ruby 1.8 和 1.9 的字符串哈希码是否相同?

c# - 在多个文件中搜索多个字符串

Python-Escaping\n 错误信息

ruby - Jekyll 博客中未加载 CSS/图像

c++ - .cpp :23: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’

c - 用C中的定界符分割字符串

JavaScript 转义序列

java - 逃避角色的艰难时光

带 SSL 选项的 Rubymine Thin