单引号字符串中的反斜杠与双引号字符串中的反斜杠

发布于 2024-07-15 06:51:08 字数 439 浏览 10 评论 0原文

如果我在双引号和单引号字符串的开头添加反斜杠+空格,则会得到不同的结果:

"\ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

哎呦我呸! 2024-07-22 06:51:09

Ruby 仅解释双引号字符串中的转义序列。 在单引号字符串中,只有 \\(反斜杠反斜杠)和 \'(反斜杠引号)被视为特殊字符。 仅当需要更多解释时才应使用双引号字符串。 否则,单引号可以提高性能。

当您提到包含变量名称时,Ruby 从来不会这样做。 只是变量名称被视为更多的字符串文字。 要包含变量(或任何表达式)的值,请像这样放置表达式:

"#{variable}"

请注意,这仅适用于双引号字符串。 要将变量添加到单引号变量中,您需要执行以下操作:

'The value of X is: '+X

如果您需要严格的格式化,请查看 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:

"#{variable}"

Note that this only works in double quoted strings. To add a variable to a single quoted one, you need to do this:

'The value of X is: '+X

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.

浮世清欢 2024-07-22 06:51:09

我建议您参考“Ruby 编程/字符串”以获得非常简洁的信息但对差异的全面概述。

来自参考:

输入“贝蒂的馅饼店”

放置“贝蒂的馅饼店”

因为“Betty's”包含一个撇号,它与单引号是相同的字符,所以在第二行我们需要使用反斜杠来转义撇号,以便 Ruby 理解撇号是在字符串文字中而不是标记字符串文字的末尾。 反斜杠后跟单引号称为转义序列。

I'd refer you to "Ruby Programming/Strings" for a very concise yet comprehensive overview of the differences.

From the reference:

puts "Betty's pie shop"

puts 'Betty\'s pie shop'

Because "Betty's" contains an apostrophe, which is the same character as the single quote, in the second line we need to use a backslash to escape the apostrophe so that Ruby understands that the apostrophe is in the string literal instead of marking the end of the string literal. The backslash followed by the single quote is called an escape sequence.

不甘平庸 2024-07-22 06:51:09

这不是完整的答案(因为已经回答了简单的问题),而是补充信息。

您喜欢哪种风格的 Ruby 字符串引用?

如有必要,请勿使用双引号
逃离他们。 并且不要陷入“单身”
vs 双引号”陷阱。Ruby 有
对任意的出色支持
字符串文字的分隔符:

http://rors.org/2008/10/26 /不要在字符串中转义

我接受了这个建议,并且从未回头!

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?

Don't use double quotes if you have to
escape them. And don't fall in "single
vs double quotes" trap. Ruby has
excellent support for arbitrary
delimiters for string literals:

http://rors.org/2008/10/26/dont-escape-in-strings

I took that advice and have never looked back!

柒夜笙歌凉 2024-07-22 06:51:09

这是因为 '\ ' 是
解释为特殊字符
双引号字符串但在
单引号字符串字符
是否按原样保留?

是的。 单引号字符串被视为文字; 双引号字符串被插入。 这在其他类似 Ruby 的语言中也是一样的,并且在 1.9 中没有改变。

Is this because the '\ ' is
interpreted as a special character in
the double quote string but in the
single quoted string the characters
are preserved as is?

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.

≈。彩虹 2024-07-22 06:51:09

你可以制作 <%= f.label :nom_entreprise, "Nom de l'entreprise" %>

you can make <%= f.label :nom_entreprise, "Nom de l'entreprise" %>

写给空气的情书 2024-07-22 06:51:08

双引号字符串支持全范围的转义序列,如下所示:

  • \a Bell/alert (0x07)
  • \b Backspace (0x08)
  • \e< /code> 转义符 (0x1b)
  • \f Formford (0x0c)
  • \n 换行符 (0x0a)
  • \r Return (0x0d)
  • \ s 空格 (0x20)
  • \t 制表符 (0x09)
  • \v 垂直制表符 (0x0b)

对于单引号字符串,两个连续的反斜杠将替换为单个反斜杠反斜杠,反斜杠后跟单引号就成为单引号:

'escape using "\\"' -> escape using "\"
'That\'s right'     -> That's right

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:

'escape using "\\"' -> escape using "\"
'That\'s right'     -> That's right
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文