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 (")双引号
\a ASCII bell (BEL)ASCII码中的bell
\b ASCII backspace (BS)ASCII中的空格
\f ASCII formfeed (FF)
\n ASCII linefeed (LF)
\r ASCII Carriage Return (CR)回车
\t ASCII Horizontal Tab (TAB)Tab键
\uxxxx Character with 16-bit hex value xxxx (Unicode only)
\Uxxxxxxxx Character with 32-bit hex value xxxxxxxx (Unicode only)
\v ASCII vertical tab (VT)
\ooo Character with octal value ooo八进制的ooo
\xhh Character with hex value hh十六进制的hh

练习代码

puts "I am 6'2\" tall." # escape double-quote inside string
puts 'I am 6\'2" tall.' # escape single-qoute inside string

tabby_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_cat
puts persian_cat
puts backslash_cat
puts fat_cat

结果:

[ufindme@ufindme day4]$ ruby ex10.rb 
I am 6'2" tall.
I am 6'2" tall.
    I'm tabbled in.
I'm splite
on a line.
I'm \a \ cat.

I'll 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 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

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

 

posted @ 2014-09-03 22:03  SchrodingerCat  阅读(150)  评论(0编辑  收藏  举报