【问题标题】:Inconsistent escaping behavior of single quote string单引号字符串的转义行为不一致
【发布时间】:2012-02-29 14:56:04
【问题描述】:

单引号字符串的转义规则在以下示例中看起来不一致: Ruby 转义单引号字符串的具体规则是什么?

p str1 = 'a\b\c'
#=> "a\\b\\c" looks fine, I know single quotes don't do escaping
p str2 = 'a\\b\\c'
#=> "a\\b\\c" hmm? It actually escapes

# Trying double quotes
p str3 = "a\b\c" 
#=> Error, \c isn't valid
p str4 = "a\\b\\c"
#=> "a\\b\\c" 

p str1 == str4, str2 == str4
# true, true

【问题讨论】:

    标签: ruby string escaping


    【解决方案1】:

    单引号字符串仅支持两个转义序列:
    \' - 单引号
    \\ - 单反斜杠
    除了这两个转义序列外,单引号之间的其他所有内容都按字面意思处理。

    来源:wikibooks

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 2023-04-02
      相关资源
      最近更新 更多