您的位置:首页 > 编程语言 > Ruby

Ruby笔记五(简单的字符串操作)

2012-01-31 16:05 176 查看
#E8.1-2.rb 字符串操作

#双引号括起来的字符串会有转义,例如:“\n” 表示换行。还有一些其它的转义符号,比如制表符之类。
str = "this is you string .\n"
puts str*2
=begin result:
this is you string .
this is you string .
=end
#单引号括起来的字符串并不会对字符串作任何解释
str2 = 'this is you string .\n '
puts str2*2 #this is you string .\n this is you string .\n

=begin 字符串内嵌表达式
在双引号扩起来的字符串中,不仅可以使用各种转义符,
而且可以放置任意的Ruby表达式在 #{ } 之中,
这些表达式在使用这个字符串的时候被计算出值,然后放入字符串
=end
def hello(name)
" Welcome, #{name} !"
end

puts hello("kaichuan") # Welcome, kaichuan !
puts hello("Ben") # Welcome, Ben !#匹配一个正则表达式,用“=~”
str="Hello,Jeriffe,Welcome!"
puts str =~ /Jeriffe/ #6
puts str =~ /f/ #10
puts str =~ /ABC/ #nil

#不匹配一个正则表达式,用“!~”
puts str !~ /Jeriffe/ # false
puts str !~ /f/ # false
puts str !~ /ABC/ # true
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: