我在哪里可以找到Ruby文档中的字符串转义序列?

时间:2013-11-02 16:14:21

标签: ruby

我可以在“Ruby Strings”和“Escape sequences”中找到有关Ruby的转义序列的详细信息。但是,在官方Ruby文档中我可以找到有关字符串转义序列的详细信息吗?

这个问题对于刚学习Ruby的人来说非常重要,因为了解如何简单地浏览文档是一个初步的挑战。

3 个答案:

答案 0 :(得分:11)

请参阅已更新的official documentation,其中包含受支持的转义字符的完整列表:

\0             null, ASCII 00h (NUL)
\a             bell, ASCII 07h (BEL)
\b             backspace, ASCII 08h (BS)
\t             horizontal tab, ASCII 09h (TAB)
\n             newline (line feed), ASCII 0Ah (LF)
\v             vertical tab, ASCII 0Bh (VT)
\f             form feed, ASCII 0Ch (FF)
\r             carriage return, ASCII 0Dh (CR)
\e             escape, ASCII 1Bh (ESC)
\s             space, ASCII 20h (SPC)
\\             backslash, \
\nnn           octal bit pattern, where nnn is 1-3 octal digits ([0-7])
\xnn           hexadecimal bit pattern, where nn is 1-2 hexadecimal digits ([0-9a-fA-F])
\unnnn         Unicode character, where nnnn is exactly 4 hexadecimal digits ([0-9a-fA-F])
\u{nnnn ...}   Unicode character(s), where each nnnn is 1-6 hexadecimal digits ([0-9a-fA-F])
\cx or \C-x    control character, where x is an ASCII printable character
\M-x           meta character, where x is an ASCII printable character
\M-\C-x        meta control character, where x is an ASCII printable character

如果您发现任何要添加或修复的内容,我们欢迎通过GitHub上的pull-request或我们的issue tracker提供反馈和贡献。

答案 1 :(得分:4)

虽然对“官方”文档的构成有不同看法,但我会提供http://www.ruby-doc.org/core-2.0.0/doc/syntax/literals_rdoc.html#label-Strings作为您问题的答案。

答案 2 :(得分:1)

下面是一些可以出现在双引号内的更常见的转义序列。

\" - 双引号("

\\ - 单反斜杠(\

\a - 铃声/警报(播放音调)

\b - 退格(删除前一个字符,将光标移回一个位置)

\r - 回车(将光标移动到行首)

\n - 换行符(将光标移动到下一行)

\s - 空格( 

\t - 标签(