码迷,mamicode.com
首页 > 其他好文 > 详细

LRTHW练习十

时间:2014-09-03 22:37:17      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   strong   ar   for   div   代码   

转义符表

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 62" tall.
I am 62" tall.
    Im tabbled in.
Im splite
on 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练习十

标签:style   blog   color   使用   strong   ar   for   div   代码   

原文地址:http://www.cnblogs.com/ufindme/p/3954705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!