在ruby中的字符串中转义单引号和双引号?

时间:2022-10-28 15:10:48

How can I escape single and double quotes in a string?

如何在字符串中转义单引号和双引号?

I want to escape single and double quotes together. I know how to pass them separately but don't know how to pass both of them.

我想要避免单引号和双引号。我知道如何分别传递它们,但不知道如何同时传递它们。

e.g: str = "ruby 'on rails" " = ruby 'on rails"

e。g: str = "ruby on rails" = ruby 'on rails"

9 个解决方案

#1


69  

My preferred way is to not worry about escaping and instead use %q, which behaves like a single-quote string (no interpolation or character escaping), or %Q for double quoted string behavior:

我的首选方法是不用担心转义,而是使用%q,它的行为类似于单引号字符串(没有插入或字符转义),或%q用于双引号字符串行为:

str = %q[ruby 'on rails" ] # like single-quoting
str2 = %Q[quoting with #{str}] # like double-quoting: will insert variable

See https://docs.ruby-lang.org/en/trunk/syntax/literals_rdoc.html#label-Strings and search for % strings.

参见https://docs.ruby-lang.org/en/trunk/syntax/literals_rdoc.html#标签- string并搜索% string。

#2


25  

Use backslash to escape characters

使用反斜杠来转义字符

str = "ruby \'on rails\" "

#3


17  

You can use Q strings which allow you to use any delimiter you like:

您可以使用Q字符串,允许您使用任何您喜欢的分隔符:

str = %Q|ruby 'on rails" " = ruby 'on rails|

#4


15  

Here is a complete list:

以下是完整的列表:

在ruby中的字符串中转义单引号和双引号?

From http://learnrubythehardway.org/book/ex10.html

从http://learnrubythehardway.org/book/ex10.html

#5


7  

>> str = "ruby 'on rails\" \" = ruby 'on rails"
=> "ruby 'on rails" " = ruby 'on rails"

#6


6  

I would go with a heredoc if I'm starting to have to worry about escaping. It will take care of it for you:

如果我开始担心逃跑,我会选择赫里多医生。它会照顾好你:

string = <<MARKER 
I don't have to "worry" about escaping!!'"!!
MARKER

MARKER delineates the start/end of the string. start string on the next line after opening the heredoc, then end the string by using the delineator again on it's own line.

标记描述字符串的开始/结束。打开heredoc后,在下一行上启动字符串,然后在它自己的行上再次使用描述器结束字符串。

This does all the escaping needed and converts to a double quoted string:

这将完成所有转义所需,并转换为双引号字符串:

string
=> "I don't have to \"worry\" about escaping!!'\"!!\n"

#7


5  

I would use just: str = %(ruby 'on rails ") Because just % stands for double quotes(or %Q) and allows interpolation of variables on the string.

我只使用:str = %(ruby 'on rails '),因为%表示双引号(或%Q),并允许在字符串中插入变量。

#8


1  

One caveat:

一个警告:

Using %Q[] and %q[] for string comparisons is not intuitively safe.

使用%Q[]和%Q[]进行字符串比较在直觉上是不安全的。

For example, if you load something meant to signify something empty, like "" or '', you need to use the actual escape sequences. For example, let's say qvar equals "" instead of any empty string.

例如,如果您装载了一些表示为空的东西,如“”或“”,则需要使用实际的转义序列。例如,假设qvar = ""而不是任何空字符串。

This will evaluate to false
if qvar == "%Q[]"

如果qvar = "%Q[]",则该值将为false

As will this,
if qvar == %Q[]

同样,如果qvar = %Q[]

While this will evaluate to true
if qvar == "\"\""

如果qvar = "\"\" \"

I ran into this issue when sending command-line vars from a different stack to my ruby script. Only Gabriel Augusto's answer worked for me.

当从另一个堆栈向ruby脚本发送命令行vars时,我遇到了这个问题。只有加布里埃尔·奥古斯托的回答对我有效。

#9


0  

Here is an example of how to use %Q[] in a more complex scenario:

下面是如何在更复杂的场景中使用%Q[]的示例:

  %Q[
    <meta property="og:title" content="#{@title}" />
    <meta property="og:description" content="#{@fullname}'s profile. #{@fullname}'s location, ranking, outcomes, and more." />
  ].html_safe

#1


69  

My preferred way is to not worry about escaping and instead use %q, which behaves like a single-quote string (no interpolation or character escaping), or %Q for double quoted string behavior:

我的首选方法是不用担心转义,而是使用%q,它的行为类似于单引号字符串(没有插入或字符转义),或%q用于双引号字符串行为:

str = %q[ruby 'on rails" ] # like single-quoting
str2 = %Q[quoting with #{str}] # like double-quoting: will insert variable

See https://docs.ruby-lang.org/en/trunk/syntax/literals_rdoc.html#label-Strings and search for % strings.

参见https://docs.ruby-lang.org/en/trunk/syntax/literals_rdoc.html#标签- string并搜索% string。

#2


25  

Use backslash to escape characters

使用反斜杠来转义字符

str = "ruby \'on rails\" "

#3


17  

You can use Q strings which allow you to use any delimiter you like:

您可以使用Q字符串,允许您使用任何您喜欢的分隔符:

str = %Q|ruby 'on rails" " = ruby 'on rails|

#4


15  

Here is a complete list:

以下是完整的列表:

在ruby中的字符串中转义单引号和双引号?

From http://learnrubythehardway.org/book/ex10.html

从http://learnrubythehardway.org/book/ex10.html

#5


7  

>> str = "ruby 'on rails\" \" = ruby 'on rails"
=> "ruby 'on rails" " = ruby 'on rails"

#6


6  

I would go with a heredoc if I'm starting to have to worry about escaping. It will take care of it for you:

如果我开始担心逃跑,我会选择赫里多医生。它会照顾好你:

string = <<MARKER 
I don't have to "worry" about escaping!!'"!!
MARKER

MARKER delineates the start/end of the string. start string on the next line after opening the heredoc, then end the string by using the delineator again on it's own line.

标记描述字符串的开始/结束。打开heredoc后,在下一行上启动字符串,然后在它自己的行上再次使用描述器结束字符串。

This does all the escaping needed and converts to a double quoted string:

这将完成所有转义所需,并转换为双引号字符串:

string
=> "I don't have to \"worry\" about escaping!!'\"!!\n"

#7


5  

I would use just: str = %(ruby 'on rails ") Because just % stands for double quotes(or %Q) and allows interpolation of variables on the string.

我只使用:str = %(ruby 'on rails '),因为%表示双引号(或%Q),并允许在字符串中插入变量。

#8


1  

One caveat:

一个警告:

Using %Q[] and %q[] for string comparisons is not intuitively safe.

使用%Q[]和%Q[]进行字符串比较在直觉上是不安全的。

For example, if you load something meant to signify something empty, like "" or '', you need to use the actual escape sequences. For example, let's say qvar equals "" instead of any empty string.

例如,如果您装载了一些表示为空的东西,如“”或“”,则需要使用实际的转义序列。例如,假设qvar = ""而不是任何空字符串。

This will evaluate to false
if qvar == "%Q[]"

如果qvar = "%Q[]",则该值将为false

As will this,
if qvar == %Q[]

同样,如果qvar = %Q[]

While this will evaluate to true
if qvar == "\"\""

如果qvar = "\"\" \"

I ran into this issue when sending command-line vars from a different stack to my ruby script. Only Gabriel Augusto's answer worked for me.

当从另一个堆栈向ruby脚本发送命令行vars时,我遇到了这个问题。只有加布里埃尔·奥古斯托的回答对我有效。

#9


0  

Here is an example of how to use %Q[] in a more complex scenario:

下面是如何在更复杂的场景中使用%Q[]的示例:

  %Q[
    <meta property="og:title" content="#{@title}" />
    <meta property="og:description" content="#{@fullname}'s profile. #{@fullname}'s location, ranking, outcomes, and more." />
  ].html_safe