如何在字符串中使用转义字符 [英] How to use escape characters in strings

查看:280
本文介绍了如何在字符串中使用转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究 Ruby Koans,但对转义子句和单引号字符串"示例感到困惑.

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

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

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. 单引号有时可以与转义字符一起使用.
  2. assert_equal"\\\'" 时,为什么字符串大小为2?(我个人认为答案是 "\'",这在大小方面更有意义).
  1. Single quotes can sometimes be used with escape characters.
  2. 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).

推荐答案

你可以把你的 string 分成两部分来澄清事情:

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

string = '\\' + '\''

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

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.

单引号字符在双引号字符串文字中没有特殊含义,因此您可以毫无困难地说出 "'".但是,反斜杠在双引号字符串中确实具有特殊含义,因此您必须说 "\\" 才能在双引号字符串中添加一个反斜杠.

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.

考虑你对 "\'" 的猜测.单引号在双引号字符串中没有特殊含义,转义不需要转义的东西只会给你东西;因此,如果 c 是一个不需要在双引号字符串中转义的字符,那么 \c 将只是 c.特别是,"\'" 的计算结果为 "'"(即双引号字符串中的一个单引号).

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).

结果是:

  • '\\\'' == "\\'"
  • "\\\"" == '\\"'
  • "\'" == '\''
  • "\'" == "'"
  • '\\\''.length == 2
  • "\\\"".length == 2
  • "\'".length == 1
  • "'".length == 1

Kassym 提供的 维基教科书参考涵盖了这些内容.

The Wikibooks reference that Kassym gave covers these things.

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

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.

这篇关于如何在字符串中使用转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆