如何使用字符串中的转义字符?

时间:2022-06-01 19:39:06

I've been working my way through the Ruby Koans and am confused by the "escape clauses and single quoted strings" examples.

我一直在使用Ruby Koans,并对“转义子句和单引号字符串”示例感到困惑。

One example shows that I can't really use escape characters in this way, but immediately after, the following example is given:

一个例子表明我不能用这种方式使用转义字符,但是紧接着,下面的例子给出了:

def test_single_quotes_sometimes_interpret_escape_characters
  string = '\\\''
  assert_equal 2, string.size # <-- my answer is correct according to the program
  assert_equal "\\'", string  # <-- my answer is correct according to the program
end

This has confused me on two fronts:

这让我在两个方面感到困惑:

  1. Single quotes can sometimes be used with escape characters.
  2. 单引号有时可以与转义字符一起使用。
  3. Why is the string size 2, when assert_equal is "\\\'"? (I personally thought the answer was "\'", which would make more sense with regards to size).
  4. 当assert_equal是“\\”时,为什么字符串大小为2 ?(我个人认为答案是“\”,这在尺寸方面更有意义)。

2 个解决方案

#1


11  

You can break your string into two pieces to clarify things:

你可以把绳子分成两段来澄清:

string = '\\' + '\''

Each part is a string of length one; '\\' is the single character \ and '\'' is the single character '. When you put them together you get the two character string \'.

每个部分都是长度为1的字符串;“\”是单字\“\”是单字\“\”是单字\当你把它们放在一起,你会得到两个字符串\'。

There are two characters that are special within a single quoted string literal: the backslash and the single quote itself. The single quote character is, of course, used to delimit the string so you need something special to get a single quote into a single quoted string, the something special is the backslash so '\'' is a single quoted string literal that represents a string containing one single quote character. Similarly, if you need to get a backslash into a single quoted string literal you escape it with another backslash so '\\' has length one and contains one backslash.

有两个字符在一个引用的字符串文本中是特殊的:反斜杠和单引号本身。当然,单引号字符用于分隔字符串,因此您需要一些特殊的东西来将单引号转换为单引号字符串,而特殊的是反斜杠,所以'\ "是一个单引号字符串文字,它表示包含一个单引号字符的字符串。类似地,如果您需要将一个反斜杠转换为一个引用的字符串文字,您可以使用另一个反斜杠来转义它,因此'\\'的长度为1,包含一个反斜杠。

The single quote character has no special meaning within a double quoted string literal so you can say "'" without any difficulty. The backslash, however, does have a special meaning in double quoted strings so you have to say "\\" to get a single backslash into your double quoted string.

单引号字符在双引号字符串文字中没有特殊含义,因此您可以毫无困难地说“'”。然而,反斜杠在双引号的字符串中有特殊的含义,所以你必须说“\”才能在双引号的字符串中得到一个反斜杠。

Consider your guess off "\'". The single quote has no special meaning within a double quoted string and escaping something that doesn't need escaping just gives you your something back; so, if c is a character that doesn't need to be escaped within a double quoted string, then \c will be just c. In particular, "\'" evaluates to "'" (i.e. one single quote within a double quoted string).

考虑你的猜测“\”。单引号在双引号中没有特殊含义,而转义不需要转义的东西就会把你的东西还给你;因此,如果c是一个不需要在双引号字符串中转义的字符,那么c就是c。

The result is that:

结果是:

  • '\\\'' == "\\'"
  • ' \ \ \ " = = " \ \ " "
  • "\\\"" == '\\"'
  • “\ \ \”“= =”\ \”
  • "\'" == '\''
  • = = ' \“\ '
  • "\'" == "'"
  • “\“= =””
  • '\\\''.length == 2
  • ' \ \ \”。长度= = 2
  • "\\\"".length == 2
  • " \ \ \ "”。长度= = 2
  • "\'".length == 1
  • “\”。长度= = 1
  • "'".length == 1
  • “‘”。长度= = 1

The Wikibooks reference that Kassym gave covers these things.

Kassym提供的*参考涵盖了这些内容。

I usually switch to %q{} (similar to single quoting) or %Q{} (similar to double quoting) when I need to get quotes into strings, all the backslashes make my eyes bleed.

当我需要将引号转换成字符串时,我通常会切换到%q{}(类似于单引号)或%q{}(类似于双引号),所有的反斜杠都会让我流血。

#2


0  

This might be worth a read : http://en.wikibooks.org/wiki/Ruby_Programming/Strings

这可能值得一读:http://en.wikibooks.org/wiki/Ruby_Programming/Strings

ruby-1.9.3-p0 :002 > a = '\\\''
 => "\\'" 
ruby-1.9.3-p0 :003 > a.size
 => 2 
ruby-1.9.3-p0 :004 > puts a
\'

In single quotes there are only two escape characters : \\ and \'.

在单引号中只有两个转义字符:\和\'。

#1


11  

You can break your string into two pieces to clarify things:

你可以把绳子分成两段来澄清:

string = '\\' + '\''

Each part is a string of length one; '\\' is the single character \ and '\'' is the single character '. When you put them together you get the two character string \'.

每个部分都是长度为1的字符串;“\”是单字\“\”是单字\“\”是单字\当你把它们放在一起,你会得到两个字符串\'。

There are two characters that are special within a single quoted string literal: the backslash and the single quote itself. The single quote character is, of course, used to delimit the string so you need something special to get a single quote into a single quoted string, the something special is the backslash so '\'' is a single quoted string literal that represents a string containing one single quote character. Similarly, if you need to get a backslash into a single quoted string literal you escape it with another backslash so '\\' has length one and contains one backslash.

有两个字符在一个引用的字符串文本中是特殊的:反斜杠和单引号本身。当然,单引号字符用于分隔字符串,因此您需要一些特殊的东西来将单引号转换为单引号字符串,而特殊的是反斜杠,所以'\ "是一个单引号字符串文字,它表示包含一个单引号字符的字符串。类似地,如果您需要将一个反斜杠转换为一个引用的字符串文字,您可以使用另一个反斜杠来转义它,因此'\\'的长度为1,包含一个反斜杠。

The single quote character has no special meaning within a double quoted string literal so you can say "'" without any difficulty. The backslash, however, does have a special meaning in double quoted strings so you have to say "\\" to get a single backslash into your double quoted string.

单引号字符在双引号字符串文字中没有特殊含义,因此您可以毫无困难地说“'”。然而,反斜杠在双引号的字符串中有特殊的含义,所以你必须说“\”才能在双引号的字符串中得到一个反斜杠。

Consider your guess off "\'". The single quote has no special meaning within a double quoted string and escaping something that doesn't need escaping just gives you your something back; so, if c is a character that doesn't need to be escaped within a double quoted string, then \c will be just c. In particular, "\'" evaluates to "'" (i.e. one single quote within a double quoted string).

考虑你的猜测“\”。单引号在双引号中没有特殊含义,而转义不需要转义的东西就会把你的东西还给你;因此,如果c是一个不需要在双引号字符串中转义的字符,那么c就是c。

The result is that:

结果是:

  • '\\\'' == "\\'"
  • ' \ \ \ " = = " \ \ " "
  • "\\\"" == '\\"'
  • “\ \ \”“= =”\ \”
  • "\'" == '\''
  • = = ' \“\ '
  • "\'" == "'"
  • “\“= =””
  • '\\\''.length == 2
  • ' \ \ \”。长度= = 2
  • "\\\"".length == 2
  • " \ \ \ "”。长度= = 2
  • "\'".length == 1
  • “\”。长度= = 1
  • "'".length == 1
  • “‘”。长度= = 1

The Wikibooks reference that Kassym gave covers these things.

Kassym提供的*参考涵盖了这些内容。

I usually switch to %q{} (similar to single quoting) or %Q{} (similar to double quoting) when I need to get quotes into strings, all the backslashes make my eyes bleed.

当我需要将引号转换成字符串时,我通常会切换到%q{}(类似于单引号)或%q{}(类似于双引号),所有的反斜杠都会让我流血。

#2


0  

This might be worth a read : http://en.wikibooks.org/wiki/Ruby_Programming/Strings

这可能值得一读:http://en.wikibooks.org/wiki/Ruby_Programming/Strings

ruby-1.9.3-p0 :002 > a = '\\\''
 => "\\'" 
ruby-1.9.3-p0 :003 > a.size
 => 2 
ruby-1.9.3-p0 :004 > puts a
\'

In single quotes there are only two escape characters : \\ and \'.

在单引号中只有两个转义字符:\和\'。