单引号字符串中的反斜杠与双引号字符串中的反斜杠
如果我在双引号和单引号字符串的开头添加反斜杠+空格,则会得到不同的结果:
"\ text"
'\ text'
在双引号字符串的输出中,我只看到一个空格。
在单引号字符串的输出中,我看到反斜杠+空格。
那里发生了什么事? 这是因为 '\ '
在双引号字符串中被解释为特殊字符,但在单引号字符串中字符按原样保留?
如果我将字符串更改为此,我会看到相同的输出,即单个斜杠后跟一个空格,然后是文本:
"\\ text"
'\\ text'
在这两种情况下,反斜杠都被转义。 我很困惑为什么他们在这种情况下以同样的方式工作。
是否有一些规则可以帮助解释单引号字符串和双引号字符串在 Ruby 中处理反斜杠的方式之间的根本区别?
If I add a backslash+space to the start of double and single quoted strings, I get different results:
"\ text"
'\ text'
In the output for the double quoted string I see only a space.
In the output for the single quoted string I see backslash+space.
What's happening there? Is this because '\ '
is interpreted as a special character in the double quote string but in the single quoted string the characters are preserved as is?
If I change the strings to this, I see the same output, namely a single slash followed by a space and then the text:
"\\ text"
'\\ text'
In both cases the backslash is escaped. I'm confused why they work the same way in this situation.
Is there some rule that would help to explain the fundamental difference between how single quoted strings and double quoted strings handle backslashes in Ruby?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Ruby 仅解释双引号字符串中的转义序列。 在单引号字符串中,只有
\\
(反斜杠反斜杠)和\'
(反斜杠引号)被视为特殊字符。 仅当需要更多解释时才应使用双引号字符串。 否则,单引号可以提高性能。当您提到包含变量名称时,Ruby 从来不会这样做。 只是变量名称被视为更多的字符串文字。 要包含变量(或任何表达式)的值,请像这样放置表达式:
请注意,这仅适用于双引号字符串。 要将变量添加到单引号变量中,您需要执行以下操作:
如果您需要严格的格式化,请查看 Ruby 版本的 sprintf 和 printf。 它们几乎是 C 函数的包装器,并且非常强大,但使用起来有点麻烦。
Ruby only interprets escape sequences in double quoted strings. In a single quoted string, only
\\
(backslash backslash) and\'
(backslash quote) are taken as special characters. You should use double quoted strings only when you need more interpretation. Otherwise, single quotes provide a performance boost.When you mentioned including the name of a variable, Ruby never does that. Just the variable name is treated as more of the string literal. To include the value of a variable (or any expression) put the expression in like this:
Note that this only works in double quoted strings. To add a variable to a single quoted one, you need to do this:
If you need serious formatting, look into Ruby's version of sprintf and printf. They are pretty much wrappers around the C functions, and are quite powerful, but a bit cumbersome to use.
我建议您参考“Ruby 编程/字符串”以获得非常简洁的信息但对差异的全面概述。
来自参考:
I'd refer you to "Ruby Programming/Strings" for a very concise yet comprehensive overview of the differences.
From the reference:
这不是完整的答案(因为已经回答了简单的问题),而是补充信息。
您喜欢哪种风格的 Ruby 字符串引用?
我接受了这个建议,并且从未回头!
This is not a full answer (since the simple question has been answered already), but rather it is supplementary information.
Which style of Ruby string quoting do you favour?
I took that advice and have never looked back!
是的。 单引号字符串被视为文字; 双引号字符串被插入。 这在其他类似 Ruby 的语言中也是一样的,并且在 1.9 中没有改变。
Yes. Single-quoted strings are treated as literals; double-quoted strings are interpolated. This is the same in other Ruby-like languages, and hasn't changed in 1.9.
你可以制作
<%= f.label :nom_entreprise, "Nom de l'entreprise" %>
you can make
<%= f.label :nom_entreprise, "Nom de l'entreprise" %>
双引号字符串支持全范围的转义序列,如下所示:
\a
Bell/alert (0x07)\b
Backspace (0x08)\e< /code> 转义符 (0x1b)
\f
Formford (0x0c)\n
换行符 (0x0a)\r
Return (0x0d)\ s
空格 (0x20)\t
制表符 (0x09)\v
垂直制表符 (0x0b)对于单引号字符串,两个连续的反斜杠将替换为单个反斜杠反斜杠,反斜杠后跟单引号就成为单引号:
Double-quoted strings support the full range of escape sequences, as shown below:
\a
Bell/alert (0x07)\b
Backspace (0x08)\e
Escape (0x1b)\f
Formford (0x0c)\n
Newline (0x0a)\r
Return (0x0d)\s
Space (0x20)\t
Tab (0x09)\v
Vertical tab (0x0b)For single-quoted strings, two consecutive backslashes are replaced by a single backslash, and a backslash followed by a single quote becomes a single quote: