翔如菲菲

其实天很蓝,阴云总会散;其实海不宽,此岸连彼岸.

导航

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

#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 

posted on 2012-01-31 16:05  翔如飞飞  阅读(287)  评论(0编辑  收藏  举报