首页 > 代码库 > LRTHW练习十

LRTHW练习十

转义符表

This all of the escape sequences Ruby supports. You may not use many of these, but memorize their format and what they do anyway. Try them out in some strings to see if you can make them work.

EscapeWhat it does.
\\Backslash () 反斜杠“\”
\‘Single-quote (‘)单引号
\"Double-quote (")双引号
\aASCII bell (BEL)ASCII码中的bell
\bASCII backspace (BS)ASCII中的空格
\fASCII formfeed (FF)
\nASCII linefeed (LF)
\r ASCIICarriage Return (CR)回车
\t ASCIIHorizontal Tab (TAB)Tab键
\uxxxxCharacter with 16-bit hex value xxxx (Unicode only)
\UxxxxxxxxCharacter with 32-bit hex value xxxxxxxx (Unicode only)
\vASCII vertical tab (VT)
\oooCharacter with octal value ooo八进制的ooo
\xhhCharacter with hex value hh十六进制的hh

练习代码

puts "I am 6‘2\" tall." # escape double-quote inside stringputs I am 6\‘2" tall. # escape single-qoute inside stringtabby_cat = "\tI‘m tabbled in."persian_cat = "I‘m splite\non a line."backslash_cat = "I‘m \\a \\ cat."fat_cat = """I‘ll do a list:\t* Cat food\t* Fishies\t* Catnip\n\t* Grass"""puts tabby_catputs persian_catputs backslash_catputs fat_cat

结果:

[ufindme@ufindme day4]$ ruby ex10.rb I am 62" tall.I am 62" tall.    Im tabbled in.Im spliteon a line.Im \a \ cat.Ill do a list:    * Cat food    * Fishies    * Catnip    * Grass

使用三个单引号(‘‘‘)代替三个双引号(""")

read_list = ‘‘‘this is new year\‘s read list(just maybe, not very sure, I guess...)\t* Camille\t* Madame Bovary\t* The Red and the Black\t* True Blood‘‘‘

显示的结果是:

this is new years read list(just maybe, not very sure, I guess...)\t* Camille\t* Madame Bovary\t* The Red and the Black\t* True Blood

原样显示,这也是以前说提到的双引号和单引号在字符串中使用的区别

 

LRTHW练习十