首页 > 代码库 > 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.
Escape | What 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 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 6‘2" tall.I am 6‘2" tall. I‘m tabbled in.I‘m spliteon 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
原样显示,这也是以前说提到的双引号和单引号在字符串中使用的区别
LRTHW练习十
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。